Refactor checking for sub-vector intrinsics.

Bug swiftshader:48

Change-Id: I05352fd64cb2e5a929295ff6a8f6196da7fdbb4b
Reviewed-on: https://swiftshader-review.googlesource.com/10949
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Reactor/Optimizer.cpp b/src/Reactor/Optimizer.cpp
index 38e24ef..4fd5d2f 100644
--- a/src/Reactor/Optimizer.cpp
+++ b/src/Reactor/Optimizer.cpp
@@ -38,6 +38,8 @@
 		void deleteInstruction(Ice::Inst *instruction);
 		bool isDead(Ice::Inst *instruction);
 
+		static const Ice::InstIntrinsicCall *asLoadSubVector(const Ice::Inst *instruction);
+		static const Ice::InstIntrinsicCall *asStoreSubVector(const Ice::Inst *instruction);
 		static bool isLoad(const Ice::Inst &instruction);
 		static bool isStore(const Ice::Inst &instruction);
 		static Ice::Operand *storeAddress(const Ice::Inst *instruction);
@@ -464,6 +466,32 @@
 		return false;
 	}
 
+	const Ice::InstIntrinsicCall *Optimizer::asLoadSubVector(const Ice::Inst *instruction)
+	{
+		if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsicCall>(instruction))
+		{
+			if(instrinsic->getIntrinsicInfo().ID == Ice::Intrinsics::LoadSubVector)
+			{
+				return instrinsic;
+			}
+		}
+
+		return nullptr;
+	}
+
+	const Ice::InstIntrinsicCall *Optimizer::asStoreSubVector(const Ice::Inst *instruction)
+	{
+		if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsicCall>(instruction))
+		{
+			if(instrinsic->getIntrinsicInfo().ID == Ice::Intrinsics::StoreSubVector)
+			{
+				return instrinsic;
+			}
+		}
+
+		return nullptr;
+	}
+
 	bool Optimizer::isLoad(const Ice::Inst &instruction)
 	{
 		if(llvm::isa<Ice::InstLoad>(&instruction))
@@ -471,12 +499,7 @@
 			return true;
 		}
 
-		if(auto intrinsicCall = llvm::dyn_cast<Ice::InstIntrinsicCall>(&instruction))
-		{
-			return intrinsicCall->getIntrinsicInfo().ID == Ice::Intrinsics::LoadSubVector;
-		}
-
-		return false;
+		return asLoadSubVector(&instruction) != nullptr;
 	}
 
 	bool Optimizer::isStore(const Ice::Inst &instruction)
@@ -486,12 +509,7 @@
 			return true;
 		}
 
-		if(auto intrinsicCall = llvm::dyn_cast<Ice::InstIntrinsicCall>(&instruction))
-		{
-			return intrinsicCall->getIntrinsicInfo().ID == Ice::Intrinsics::StoreSubVector;
-		}
-
-		return false;
+		return asStoreSubVector(&instruction) != nullptr;
 	}
 
 	Ice::Operand *Optimizer::storeAddress(const Ice::Inst *instruction)
@@ -503,12 +521,9 @@
 			return store->getAddr();
 		}
 
-		if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsicCall>(instruction))
+		if(auto *storeSubVector = asStoreSubVector(instruction))
 		{
-			if(instrinsic->getIntrinsicInfo().ID == Ice::Intrinsics::StoreSubVector)
-			{
-				return instrinsic->getSrc(2);
-			}
+			return storeSubVector->getSrc(2);
 		}
 
 		return nullptr;
@@ -523,12 +538,9 @@
 			return load->getSourceAddress();
 		}
 
-		if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsicCall>(instruction))
+		if(auto *loadSubVector = asLoadSubVector(instruction))
 		{
-			if(instrinsic->getIntrinsicInfo().ID == Ice::Intrinsics::LoadSubVector)
-			{
-				return instrinsic->getSrc(1);
-			}
+			return loadSubVector->getSrc(1);
 		}
 
 		return nullptr;
@@ -543,12 +555,9 @@
 			return store->getData();
 		}
 
-		if(auto *instrinsic = llvm::dyn_cast<Ice::InstIntrinsicCall>(instruction))
+		if(auto *storeSubVector = asStoreSubVector(instruction))
 		{
-			if(instrinsic->getIntrinsicInfo().ID == Ice::Intrinsics::StoreSubVector)
-			{
-				return instrinsic->getSrc(1);
-			}
+			return storeSubVector->getSrc(1);
 		}
 
 		return nullptr;