Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Support/CallSite.h" |
| 21 | using namespace llvm; |
| 22 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 23 | unsigned CallSite::getCallingConv() const { |
| 24 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 25 | return CI->getCallingConv(); |
| 26 | else |
| 27 | return cast<InvokeInst>(I)->getCallingConv(); |
| 28 | } |
| 29 | void CallSite::setCallingConv(unsigned CC) { |
| 30 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 31 | CI->setCallingConv(CC); |
| 32 | else |
| 33 | cast<InvokeInst>(I)->setCallingConv(CC); |
| 34 | } |
| 35 | |
| 36 | |
Chris Lattner | 70aa33e | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 37 | |
| 38 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 40 | // TerminatorInst Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | Use *Ops, unsigned NumOps, Instruction *IB) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 45 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) { |
| 46 | } |
| 47 | |
| 48 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
| 49 | Use *Ops, unsigned NumOps, BasicBlock *IAE) |
| 50 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) { |
| 51 | } |
| 52 | |
Chris Lattner | 70aa33e | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 53 | // Out of line virtual method, so the vtable, etc has a home. |
| 54 | TerminatorInst::~TerminatorInst() { |
| 55 | } |
| 56 | |
| 57 | // Out of line virtual method, so the vtable, etc has a home. |
| 58 | UnaryInstruction::~UnaryInstruction() { |
| 59 | } |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // PHINode Class |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | PHINode::PHINode(const PHINode &PN) |
| 67 | : Instruction(PN.getType(), Instruction::PHI, |
| 68 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 69 | ReservedSpace(PN.getNumOperands()) { |
| 70 | Use *OL = OperandList; |
| 71 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 72 | OL[i].init(PN.getOperand(i), this); |
| 73 | OL[i+1].init(PN.getOperand(i+1), this); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | PHINode::~PHINode() { |
| 78 | delete [] OperandList; |
| 79 | } |
| 80 | |
| 81 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 82 | // predecessor basic block is deleted. |
| 83 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 84 | unsigned NumOps = getNumOperands(); |
| 85 | Use *OL = OperandList; |
| 86 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 87 | Value *Removed = OL[Idx*2]; |
| 88 | |
| 89 | // Move everything after this operand down. |
| 90 | // |
| 91 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 92 | // client might not expect this to happen. The code as it is thrashes the |
| 93 | // use/def lists, which is kinda lame. |
| 94 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 95 | OL[i-2] = OL[i]; |
| 96 | OL[i-2+1] = OL[i+1]; |
| 97 | } |
| 98 | |
| 99 | // Nuke the last value. |
| 100 | OL[NumOps-2].set(0); |
| 101 | OL[NumOps-2+1].set(0); |
| 102 | NumOperands = NumOps-2; |
| 103 | |
| 104 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 105 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 106 | // If anyone is using this PHI, make them use a dummy value instead... |
| 107 | replaceAllUsesWith(UndefValue::get(getType())); |
| 108 | eraseFromParent(); |
| 109 | } |
| 110 | return Removed; |
| 111 | } |
| 112 | |
| 113 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 114 | /// list according to the following behavior: |
| 115 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 116 | /// of operation. This grows the number of ops by 1.5 times. |
| 117 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 118 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 119 | /// |
| 120 | void PHINode::resizeOperands(unsigned NumOps) { |
| 121 | if (NumOps == 0) { |
| 122 | NumOps = (getNumOperands())*3/2; |
| 123 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 124 | } else if (NumOps*2 > NumOperands) { |
| 125 | // No resize needed. |
| 126 | if (ReservedSpace >= NumOps) return; |
| 127 | } else if (NumOps == NumOperands) { |
| 128 | if (ReservedSpace == NumOps) return; |
| 129 | } else { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 130 | return; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ReservedSpace = NumOps; |
| 134 | Use *NewOps = new Use[NumOps]; |
| 135 | Use *OldOps = OperandList; |
| 136 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 137 | NewOps[i].init(OldOps[i], this); |
| 138 | OldOps[i].set(0); |
| 139 | } |
| 140 | delete [] OldOps; |
| 141 | OperandList = NewOps; |
| 142 | } |
| 143 | |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 144 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 145 | /// value, return the value, otherwise return null. |
| 146 | /// |
Chris Lattner | 9acbd61 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 147 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 148 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 149 | if (getNumIncomingValues() == 1) |
Chris Lattner | d7231ac | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 150 | if (getIncomingValue(0) != this) // not X = phi X |
| 151 | return getIncomingValue(0); |
| 152 | else |
| 153 | return UndefValue::get(getType()); // Self cycle is dead. |
| 154 | |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 155 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 156 | // the PHI node with the incoming value. |
| 157 | // |
| 158 | Value *InVal = 0; |
Chris Lattner | b3b48e1 | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 159 | bool HasUndefInput = false; |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 160 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | b3b48e1 | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 161 | if (isa<UndefValue>(getIncomingValue(i))) |
| 162 | HasUndefInput = true; |
| 163 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 164 | if (InVal && getIncomingValue(i) != InVal) |
| 165 | return 0; // Not the same, bail out. |
| 166 | else |
| 167 | InVal = getIncomingValue(i); |
| 168 | |
| 169 | // The only case that could cause InVal to be null is if we have a PHI node |
| 170 | // that only has entries for itself. In this case, there is no entry into the |
| 171 | // loop, so kill the PHI. |
| 172 | // |
| 173 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 174 | |
Chris Lattner | b3b48e1 | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 175 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 176 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 177 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 178 | // or if the client is prepared to deal with this possibility. |
| 179 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 180 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 181 | // If it's in the entry block, it dominates everything. |
Chris Lattner | 8313a25 | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 182 | if (IV->getParent() != &IV->getParent()->getParent()->front() || |
| 183 | isa<InvokeInst>(IV)) |
Chris Lattner | b3b48e1 | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 184 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 185 | |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 186 | // All of the incoming values are the same, return the value now. |
| 187 | return InVal; |
| 188 | } |
| 189 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 190 | |
| 191 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 192 | // CallInst Implementation |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 195 | CallInst::~CallInst() { |
| 196 | delete [] OperandList; |
| 197 | } |
| 198 | |
| 199 | void CallInst::init(Value *Func, const std::vector<Value*> &Params) { |
| 200 | NumOperands = Params.size()+1; |
| 201 | Use *OL = OperandList = new Use[Params.size()+1]; |
| 202 | OL[0].init(Func, this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 203 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 204 | const FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 205 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 206 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 207 | assert((Params.size() == FTy->getNumParams() || |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 208 | (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 209 | "Calling a function with bad signature!"); |
| 210 | for (unsigned i = 0, e = Params.size(); i != e; ++i) { |
| 211 | assert((i >= FTy->getNumParams() || |
| 212 | FTy->getParamType(i) == Params[i]->getType()) && |
| 213 | "Calling a function with a bad signature!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 214 | OL[i+1].init(Params[i], this); |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 215 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 218 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
| 219 | NumOperands = 3; |
| 220 | Use *OL = OperandList = new Use[3]; |
| 221 | OL[0].init(Func, this); |
| 222 | OL[1].init(Actual1, this); |
| 223 | OL[2].init(Actual2, this); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 224 | |
| 225 | const FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 226 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 227 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 228 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 229 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 230 | "Calling a function with bad signature"); |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 231 | assert((0 >= FTy->getNumParams() || |
| 232 | FTy->getParamType(0) == Actual1->getType()) && |
| 233 | "Calling a function with a bad signature!"); |
| 234 | assert((1 >= FTy->getNumParams() || |
| 235 | FTy->getParamType(1) == Actual2->getType()) && |
| 236 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 239 | void CallInst::init(Value *Func, Value *Actual) { |
| 240 | NumOperands = 2; |
| 241 | Use *OL = OperandList = new Use[2]; |
| 242 | OL[0].init(Func, this); |
| 243 | OL[1].init(Actual, this); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 244 | |
| 245 | const FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 246 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 247 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 248 | assert((FTy->getNumParams() == 1 || |
| 249 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 250 | "Calling a function with bad signature"); |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 251 | assert((0 == FTy->getNumParams() || |
| 252 | FTy->getParamType(0) == Actual->getType()) && |
| 253 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 256 | void CallInst::init(Value *Func) { |
| 257 | NumOperands = 1; |
| 258 | Use *OL = OperandList = new Use[1]; |
| 259 | OL[0].init(Func, this); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 260 | |
| 261 | const FunctionType *MTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 262 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 263 | |
| 264 | assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); |
| 265 | } |
| 266 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 267 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 268 | const std::string &Name, Instruction *InsertBefore) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 269 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 270 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 271 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 272 | init(Func, Params); |
| 273 | } |
| 274 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 275 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 276 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 277 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 278 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 279 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 280 | init(Func, Params); |
| 281 | } |
| 282 | |
| 283 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 284 | const std::string &Name, Instruction *InsertBefore) |
| 285 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 286 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 287 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 288 | init(Func, Actual1, Actual2); |
| 289 | } |
| 290 | |
| 291 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 292 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 293 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 294 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 295 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 296 | init(Func, Actual1, Actual2); |
| 297 | } |
| 298 | |
| 299 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 300 | Instruction *InsertBefore) |
| 301 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 302 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 303 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 304 | init(Func, Actual); |
| 305 | } |
| 306 | |
| 307 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 308 | BasicBlock *InsertAtEnd) |
| 309 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 310 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 311 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | init(Func, Actual); |
| 313 | } |
| 314 | |
| 315 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 316 | Instruction *InsertBefore) |
| 317 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 318 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 319 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | init(Func); |
| 321 | } |
| 322 | |
| 323 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 324 | BasicBlock *InsertAtEnd) |
| 325 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 326 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 327 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 328 | init(Func); |
| 329 | } |
| 330 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 331 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 332 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
| 333 | CI.getNumOperands()) { |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 334 | SubclassData = CI.SubclassData; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 335 | Use *OL = OperandList; |
| 336 | Use *InOL = CI.OperandList; |
| 337 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 338 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // InvokeInst Implementation |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 346 | InvokeInst::~InvokeInst() { |
| 347 | delete [] OperandList; |
| 348 | } |
| 349 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 350 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 351 | const std::vector<Value*> &Params) { |
| 352 | NumOperands = 3+Params.size(); |
| 353 | Use *OL = OperandList = new Use[3+Params.size()]; |
| 354 | OL[0].init(Fn, this); |
| 355 | OL[1].init(IfNormal, this); |
| 356 | OL[2].init(IfException, this); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 357 | const FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 358 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 359 | |
| 360 | assert((Params.size() == FTy->getNumParams()) || |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 361 | (FTy->isVarArg() && Params.size() > FTy->getNumParams()) && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 362 | "Calling a function with bad signature"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 364 | for (unsigned i = 0, e = Params.size(); i != e; i++) { |
| 365 | assert((i >= FTy->getNumParams() || |
| 366 | FTy->getParamType(i) == Params[i]->getType()) && |
| 367 | "Invoking a function with a bad signature!"); |
| 368 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 369 | OL[i+3].init(Params[i], this); |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 370 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 374 | BasicBlock *IfException, |
| 375 | const std::vector<Value*> &Params, |
| 376 | const std::string &Name, Instruction *InsertBefore) |
| 377 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 378 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 379 | Instruction::Invoke, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 380 | init(Fn, IfNormal, IfException, Params); |
| 381 | } |
| 382 | |
| 383 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 384 | BasicBlock *IfException, |
| 385 | const std::vector<Value*> &Params, |
| 386 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 387 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 388 | ->getElementType())->getReturnType(), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 389 | Instruction::Invoke, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 390 | init(Fn, IfNormal, IfException, Params); |
| 391 | } |
| 392 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 393 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 394 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 395 | new Use[II.getNumOperands()], II.getNumOperands()) { |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 396 | SubclassData = II.SubclassData; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 397 | Use *OL = OperandList, *InOL = II.OperandList; |
| 398 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 399 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 402 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 403 | return getSuccessor(idx); |
| 404 | } |
| 405 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 406 | return getNumSuccessors(); |
| 407 | } |
| 408 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 409 | return setSuccessor(idx, B); |
| 410 | } |
| 411 | |
| 412 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // ReturnInst Implementation |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 417 | void ReturnInst::init(Value *retVal) { |
| 418 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 419 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 859804f | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 420 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 421 | NumOperands = 1; |
| 422 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 859804f | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 426 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 427 | return getNumSuccessors(); |
| 428 | } |
| 429 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 430 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 431 | // emit the vtable for the class in this translation unit. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 432 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 433 | assert(0 && "ReturnInst has no successors!"); |
| 434 | } |
| 435 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 436 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 437 | assert(0 && "ReturnInst has no successors!"); |
| 438 | abort(); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 443 | //===----------------------------------------------------------------------===// |
| 444 | // UnwindInst Implementation |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 447 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 448 | return getNumSuccessors(); |
| 449 | } |
| 450 | |
| 451 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 452 | assert(0 && "UnwindInst has no successors!"); |
| 453 | } |
| 454 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 455 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 456 | assert(0 && "UnwindInst has no successors!"); |
| 457 | abort(); |
| 458 | return 0; |
| 459 | } |
| 460 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 461 | //===----------------------------------------------------------------------===// |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 462 | // UnreachableInst Implementation |
| 463 | //===----------------------------------------------------------------------===// |
| 464 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 465 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 466 | return getNumSuccessors(); |
| 467 | } |
| 468 | |
| 469 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 470 | assert(0 && "UnwindInst has no successors!"); |
| 471 | } |
| 472 | |
| 473 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 474 | assert(0 && "UnwindInst has no successors!"); |
| 475 | abort(); |
| 476 | return 0; |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 480 | // BranchInst Implementation |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 483 | void BranchInst::AssertOK() { |
| 484 | if (isConditional()) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 485 | assert(getCondition()->getType() == Type::BoolTy && |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 486 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 489 | BranchInst::BranchInst(const BranchInst &BI) : |
| 490 | TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) { |
| 491 | OperandList[0].init(BI.getOperand(0), this); |
| 492 | if (BI.getNumOperands() != 1) { |
| 493 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 494 | OperandList[1].init(BI.getOperand(1), this); |
| 495 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 499 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 500 | return getSuccessor(idx); |
| 501 | } |
| 502 | unsigned BranchInst::getNumSuccessorsV() const { |
| 503 | return getNumSuccessors(); |
| 504 | } |
| 505 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 506 | setSuccessor(idx, B); |
| 507 | } |
| 508 | |
| 509 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 510 | //===----------------------------------------------------------------------===// |
| 511 | // AllocationInst Implementation |
| 512 | //===----------------------------------------------------------------------===// |
| 513 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 514 | static Value *getAISize(Value *Amt) { |
| 515 | if (!Amt) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 516 | Amt = ConstantInt::get(Type::UIntTy, 1); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 517 | else { |
| 518 | assert(!isa<BasicBlock>(Amt) && |
| 519 | "Passed basic block into allocation size parameter! Ue other ctor"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 520 | assert(Amt->getType() == Type::UIntTy && |
| 521 | "Malloc/Allocation array size != UIntTy!"); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 522 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 523 | return Amt; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 526 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 527 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 528 | Instruction *InsertBefore) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 529 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 530 | Name, InsertBefore), Alignment(Align) { |
Chris Lattner | 6033716 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 531 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 532 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 535 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 536 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 537 | BasicBlock *InsertAtEnd) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 538 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 539 | Name, InsertAtEnd), Alignment(Align) { |
Chris Lattner | 6033716 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 540 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 541 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 70aa33e | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 544 | // Out of line virtual method, so the vtable, etc has a home. |
| 545 | AllocationInst::~AllocationInst() { |
| 546 | } |
| 547 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 548 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 549 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0))) |
| 550 | return CUI->getZExtValue() != 1; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 551 | return true; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | const Type *AllocationInst::getAllocatedType() const { |
| 555 | return getType()->getElementType(); |
| 556 | } |
| 557 | |
| 558 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 559 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 560 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | MallocInst::MallocInst(const MallocInst &MI) |
| 564 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 565 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | // FreeInst Implementation |
| 570 | //===----------------------------------------------------------------------===// |
| 571 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 572 | void FreeInst::AssertOK() { |
| 573 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 574 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 578 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) { |
| 579 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 583 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) { |
| 584 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | // LoadInst Implementation |
| 590 | //===----------------------------------------------------------------------===// |
| 591 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 592 | void LoadInst::AssertOK() { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 593 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 594 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 598 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 599 | Load, Ptr, Name, InsertBef) { |
| 600 | setVolatile(false); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 601 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 605 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 606 | Load, Ptr, Name, InsertAE) { |
| 607 | setVolatile(false); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 608 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 612 | Instruction *InsertBef) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 613 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 614 | Load, Ptr, Name, InsertBef) { |
| 615 | setVolatile(isVolatile); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 616 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 620 | BasicBlock *InsertAE) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 621 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 622 | Load, Ptr, Name, InsertAE) { |
| 623 | setVolatile(isVolatile); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 624 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | // StoreInst Implementation |
| 630 | //===----------------------------------------------------------------------===// |
| 631 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 632 | void StoreInst::AssertOK() { |
| 633 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 634 | "Ptr must have pointer type!"); |
| 635 | assert(getOperand(0)->getType() == |
| 636 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 8fabb62 | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 637 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 638 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 639 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 640 | |
| 641 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 642 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 643 | Ops[0].init(val, this); |
| 644 | Ops[1].init(addr, this); |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 645 | setVolatile(false); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 646 | AssertOK(); |
| 647 | } |
| 648 | |
| 649 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 650 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 651 | Ops[0].init(val, this); |
| 652 | Ops[1].init(addr, this); |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 653 | setVolatile(false); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 654 | AssertOK(); |
| 655 | } |
| 656 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 657 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 658 | Instruction *InsertBefore) |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 659 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 660 | Ops[0].init(val, this); |
| 661 | Ops[1].init(addr, this); |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 662 | setVolatile(isVolatile); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 663 | AssertOK(); |
| 664 | } |
| 665 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 666 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 667 | BasicBlock *InsertAtEnd) |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 668 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 669 | Ops[0].init(val, this); |
| 670 | Ops[1].init(addr, this); |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 671 | setVolatile(isVolatile); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 672 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | // GetElementPtrInst Implementation |
| 677 | //===----------------------------------------------------------------------===// |
| 678 | |
| 679 | // checkType - Simple wrapper function to give a better assertion failure |
| 680 | // message on bad indexes for a gep instruction. |
| 681 | // |
| 682 | static inline const Type *checkType(const Type *Ty) { |
Chris Lattner | ff8953a | 2006-05-14 18:34:36 +0000 | [diff] [blame] | 683 | assert(Ty && "Invalid GetElementPtrInst indices for type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 684 | return Ty; |
| 685 | } |
| 686 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 687 | void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) { |
| 688 | NumOperands = 1+Idx.size(); |
| 689 | Use *OL = OperandList = new Use[NumOperands]; |
| 690 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 691 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 692 | for (unsigned i = 0, e = Idx.size(); i != e; ++i) |
| 693 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 697 | NumOperands = 3; |
| 698 | Use *OL = OperandList = new Use[3]; |
| 699 | OL[0].init(Ptr, this); |
| 700 | OL[1].init(Idx0, this); |
| 701 | OL[2].init(Idx1, this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 704 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 705 | NumOperands = 2; |
| 706 | Use *OL = OperandList = new Use[2]; |
| 707 | OL[0].init(Ptr, this); |
| 708 | OL[1].init(Idx, this); |
| 709 | } |
| 710 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 711 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | fd67824 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 712 | const std::string &Name, Instruction *InBe) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 713 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 714 | Idx, true))), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 715 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 716 | init(Ptr, Idx); |
| 717 | } |
| 718 | |
| 719 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | fd67824 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 720 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 721 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 722 | Idx, true))), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 723 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 724 | init(Ptr, Idx); |
| 725 | } |
| 726 | |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 727 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 728 | const std::string &Name, Instruction *InBe) |
| 729 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 730 | GetElementPtr, 0, 0, Name, InBe) { |
| 731 | init(Ptr, Idx); |
| 732 | } |
| 733 | |
| 734 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 735 | const std::string &Name, BasicBlock *IAE) |
| 736 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 737 | GetElementPtr, 0, 0, Name, IAE) { |
| 738 | init(Ptr, Idx); |
| 739 | } |
| 740 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 741 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 742 | const std::string &Name, Instruction *InBe) |
| 743 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 744 | Idx0, Idx1, true))), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 745 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 746 | init(Ptr, Idx0, Idx1); |
| 747 | } |
| 748 | |
| 749 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
Misha Brukman | fd67824 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 750 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 751 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 752 | Idx0, Idx1, true))), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 753 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 754 | init(Ptr, Idx0, Idx1); |
| 755 | } |
| 756 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 757 | GetElementPtrInst::~GetElementPtrInst() { |
| 758 | delete[] OperandList; |
| 759 | } |
| 760 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 761 | // getIndexedType - Returns the type of the element that would be loaded with |
| 762 | // a load instruction with the specified parameters. |
| 763 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 764 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 765 | // pointer type. |
| 766 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 767 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 768 | const std::vector<Value*> &Idx, |
| 769 | bool AllowCompositeLeaf) { |
| 770 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 771 | |
| 772 | // Handle the special case of the empty set index set... |
| 773 | if (Idx.empty()) |
| 774 | if (AllowCompositeLeaf || |
| 775 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 776 | return cast<PointerType>(Ptr)->getElementType(); |
| 777 | else |
| 778 | return 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 779 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 780 | unsigned CurIdx = 0; |
| 781 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| 782 | if (Idx.size() == CurIdx) { |
| 783 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 784 | return 0; // Can't load a whole structure or array!?!? |
| 785 | } |
| 786 | |
| 787 | Value *Index = Idx[CurIdx++]; |
| 788 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 789 | return 0; // Can only index into pointer types at the first index! |
| 790 | if (!CT->indexValid(Index)) return 0; |
| 791 | Ptr = CT->getTypeAtIndex(Index); |
| 792 | |
| 793 | // If the new type forwards to another type, then it is in the middle |
| 794 | // of being refined to another type (and hence, may have dropped all |
| 795 | // references to what it was using before). So, use the new forwarded |
| 796 | // type. |
| 797 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 798 | Ptr = Ty; |
| 799 | } |
| 800 | } |
| 801 | return CurIdx == Idx.size() ? Ptr : 0; |
| 802 | } |
| 803 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 804 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 805 | Value *Idx0, Value *Idx1, |
| 806 | bool AllowCompositeLeaf) { |
| 807 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 808 | if (!PTy) return 0; // Type isn't a pointer type! |
| 809 | |
| 810 | // Check the pointer index. |
| 811 | if (!PTy->indexValid(Idx0)) return 0; |
| 812 | |
| 813 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 814 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 815 | |
| 816 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 817 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 818 | return ElTy; |
| 819 | return 0; |
| 820 | } |
| 821 | |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 822 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 823 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 824 | if (!PTy) return 0; // Type isn't a pointer type! |
| 825 | |
| 826 | // Check the pointer index. |
| 827 | if (!PTy->indexValid(Idx)) return 0; |
| 828 | |
Chris Lattner | f818cfe | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 829 | return PTy->getElementType(); |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 832 | //===----------------------------------------------------------------------===// |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 833 | // ExtractElementInst Implementation |
| 834 | //===----------------------------------------------------------------------===// |
| 835 | |
| 836 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 837 | const std::string &Name, |
| 838 | Instruction *InsertBef) |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 839 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 840 | ExtractElement, Ops, 2, Name, InsertBef) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 841 | assert(isValidOperands(Val, Index) && |
| 842 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 843 | Ops[0].init(Val, this); |
| 844 | Ops[1].init(Index, this); |
| 845 | } |
| 846 | |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 847 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 848 | const std::string &Name, |
| 849 | Instruction *InsertBef) |
| 850 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 851 | ExtractElement, Ops, 2, Name, InsertBef) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 852 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 853 | assert(isValidOperands(Val, Index) && |
| 854 | "Invalid extractelement instruction operands!"); |
| 855 | Ops[0].init(Val, this); |
| 856 | Ops[1].init(Index, this); |
| 857 | } |
| 858 | |
| 859 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 860 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 861 | const std::string &Name, |
| 862 | BasicBlock *InsertAE) |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 863 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 864 | ExtractElement, Ops, 2, Name, InsertAE) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 865 | assert(isValidOperands(Val, Index) && |
| 866 | "Invalid extractelement instruction operands!"); |
| 867 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 868 | Ops[0].init(Val, this); |
| 869 | Ops[1].init(Index, this); |
| 870 | } |
| 871 | |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 872 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 873 | const std::string &Name, |
| 874 | BasicBlock *InsertAE) |
| 875 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 876 | ExtractElement, Ops, 2, Name, InsertAE) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 877 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 878 | assert(isValidOperands(Val, Index) && |
| 879 | "Invalid extractelement instruction operands!"); |
| 880 | |
| 881 | Ops[0].init(Val, this); |
| 882 | Ops[1].init(Index, this); |
| 883 | } |
| 884 | |
| 885 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 886 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
| 887 | if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy) |
| 888 | return false; |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 893 | //===----------------------------------------------------------------------===// |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 894 | // InsertElementInst Implementation |
| 895 | //===----------------------------------------------------------------------===// |
| 896 | |
Chris Lattner | f95670f | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 897 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 898 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 899 | Ops[0].init(IE.Ops[0], this); |
| 900 | Ops[1].init(IE.Ops[1], this); |
| 901 | Ops[2].init(IE.Ops[2], this); |
| 902 | } |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 903 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 904 | const std::string &Name, |
| 905 | Instruction *InsertBef) |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 906 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
| 907 | assert(isValidOperands(Vec, Elt, Index) && |
| 908 | "Invalid insertelement instruction operands!"); |
| 909 | Ops[0].init(Vec, this); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 910 | Ops[1].init(Elt, this); |
| 911 | Ops[2].init(Index, this); |
| 912 | } |
| 913 | |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 914 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 915 | const std::string &Name, |
| 916 | Instruction *InsertBef) |
| 917 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 918 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 919 | assert(isValidOperands(Vec, Elt, Index) && |
| 920 | "Invalid insertelement instruction operands!"); |
| 921 | Ops[0].init(Vec, this); |
| 922 | Ops[1].init(Elt, this); |
| 923 | Ops[2].init(Index, this); |
| 924 | } |
| 925 | |
| 926 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 927 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 928 | const std::string &Name, |
| 929 | BasicBlock *InsertAE) |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 930 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
| 931 | assert(isValidOperands(Vec, Elt, Index) && |
| 932 | "Invalid insertelement instruction operands!"); |
| 933 | |
| 934 | Ops[0].init(Vec, this); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 935 | Ops[1].init(Elt, this); |
| 936 | Ops[2].init(Index, this); |
| 937 | } |
| 938 | |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 939 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 940 | const std::string &Name, |
| 941 | BasicBlock *InsertAE) |
| 942 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 943 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 944 | assert(isValidOperands(Vec, Elt, Index) && |
| 945 | "Invalid insertelement instruction operands!"); |
| 946 | |
| 947 | Ops[0].init(Vec, this); |
| 948 | Ops[1].init(Elt, this); |
| 949 | Ops[2].init(Index, this); |
| 950 | } |
| 951 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 952 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 953 | const Value *Index) { |
| 954 | if (!isa<PackedType>(Vec->getType())) |
| 955 | return false; // First operand of insertelement must be packed type. |
| 956 | |
| 957 | if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType()) |
| 958 | return false;// Second operand of insertelement must be packed element type. |
| 959 | |
| 960 | if (Index->getType() != Type::UIntTy) |
| 961 | return false; // Third operand of insertelement must be uint. |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 966 | //===----------------------------------------------------------------------===// |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 967 | // ShuffleVectorInst Implementation |
| 968 | //===----------------------------------------------------------------------===// |
| 969 | |
Chris Lattner | f95670f | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 970 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 971 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 972 | Ops[0].init(SV.Ops[0], this); |
| 973 | Ops[1].init(SV.Ops[1], this); |
| 974 | Ops[2].init(SV.Ops[2], this); |
| 975 | } |
| 976 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 977 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 978 | const std::string &Name, |
| 979 | Instruction *InsertBefore) |
| 980 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { |
| 981 | assert(isValidOperands(V1, V2, Mask) && |
| 982 | "Invalid shuffle vector instruction operands!"); |
| 983 | Ops[0].init(V1, this); |
| 984 | Ops[1].init(V2, this); |
| 985 | Ops[2].init(Mask, this); |
| 986 | } |
| 987 | |
| 988 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 989 | const std::string &Name, |
| 990 | BasicBlock *InsertAtEnd) |
| 991 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { |
| 992 | assert(isValidOperands(V1, V2, Mask) && |
| 993 | "Invalid shuffle vector instruction operands!"); |
| 994 | |
| 995 | Ops[0].init(V1, this); |
| 996 | Ops[1].init(V2, this); |
| 997 | Ops[2].init(Mask, this); |
| 998 | } |
| 999 | |
| 1000 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1001 | const Value *Mask) { |
| 1002 | if (!isa<PackedType>(V1->getType())) return false; |
| 1003 | if (V1->getType() != V2->getType()) return false; |
| 1004 | if (!isa<PackedType>(Mask->getType()) || |
| 1005 | cast<PackedType>(Mask->getType())->getElementType() != Type::UIntTy || |
| 1006 | cast<PackedType>(Mask->getType())->getNumElements() != |
| 1007 | cast<PackedType>(V1->getType())->getNumElements()) |
| 1008 | return false; |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1014 | // BinaryOperator Class |
| 1015 | //===----------------------------------------------------------------------===// |
| 1016 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1017 | void BinaryOperator::init(BinaryOps iType) |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1018 | { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1019 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
| 1020 | assert(LHS->getType() == RHS->getType() && |
| 1021 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1022 | #ifndef NDEBUG |
| 1023 | switch (iType) { |
| 1024 | case Add: case Sub: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1025 | case Mul: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1026 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1027 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | b9d4100 | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1028 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
| 1029 | isa<PackedType>(getType())) && |
Brian Gaeke | 715c90b | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1030 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1031 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1032 | case UDiv: |
| 1033 | case SDiv: |
| 1034 | assert(getType() == LHS->getType() && |
| 1035 | "Arithmetic operation should return same type as operands!"); |
| 1036 | assert((getType()->isInteger() || (isa<PackedType>(getType()) && |
| 1037 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
| 1038 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1039 | break; |
| 1040 | case FDiv: |
| 1041 | assert(getType() == LHS->getType() && |
| 1042 | "Arithmetic operation should return same type as operands!"); |
| 1043 | assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && |
| 1044 | cast<PackedType>(getType())->getElementType()->isFloatingPoint())) |
| 1045 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1046 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1047 | case URem: |
| 1048 | case SRem: |
| 1049 | assert(getType() == LHS->getType() && |
| 1050 | "Arithmetic operation should return same type as operands!"); |
| 1051 | assert((getType()->isInteger() || (isa<PackedType>(getType()) && |
| 1052 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
| 1053 | "Incorrect operand type (not integer) for S/UREM"); |
| 1054 | break; |
| 1055 | case FRem: |
| 1056 | assert(getType() == LHS->getType() && |
| 1057 | "Arithmetic operation should return same type as operands!"); |
| 1058 | assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && |
| 1059 | cast<PackedType>(getType())->getElementType()->isFloatingPoint())) |
| 1060 | && "Incorrect operand type (not floating point) for FREM"); |
| 1061 | break; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1062 | case And: case Or: |
| 1063 | case Xor: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1064 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1065 | "Logical operation should return same type as operands!"); |
Chris Lattner | b9d4100 | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1066 | assert((getType()->isIntegral() || |
| 1067 | (isa<PackedType>(getType()) && |
| 1068 | cast<PackedType>(getType())->getElementType()->isIntegral())) && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1069 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | case SetLT: case SetGT: case SetLE: |
| 1072 | case SetGE: case SetEQ: case SetNE: |
| 1073 | assert(getType() == Type::BoolTy && "Setcc must return bool!"); |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | #endif |
| 1078 | } |
| 1079 | |
| 1080 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | fd67824 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1081 | const std::string &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1082 | Instruction *InsertBefore) { |
| 1083 | assert(S1->getType() == S2->getType() && |
| 1084 | "Cannot create binary operator with two operands of differing type!"); |
| 1085 | switch (Op) { |
| 1086 | // Binary comparison operators... |
| 1087 | case SetLT: case SetGT: case SetLE: |
| 1088 | case SetGE: case SetEQ: case SetNE: |
| 1089 | return new SetCondInst(Op, S1, S2, Name, InsertBefore); |
| 1090 | |
| 1091 | default: |
| 1092 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | fd67824 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1097 | const std::string &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1098 | BasicBlock *InsertAtEnd) { |
| 1099 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1100 | InsertAtEnd->getInstList().push_back(Res); |
| 1101 | return Res; |
| 1102 | } |
| 1103 | |
| 1104 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1105 | Instruction *InsertBefore) { |
| 1106 | if (!Op->getType()->isFloatingPoint()) |
| 1107 | return new BinaryOperator(Instruction::Sub, |
| 1108 | Constant::getNullValue(Op->getType()), Op, |
| 1109 | Op->getType(), Name, InsertBefore); |
| 1110 | else |
| 1111 | return new BinaryOperator(Instruction::Sub, |
| 1112 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1113 | Op->getType(), Name, InsertBefore); |
| 1114 | } |
| 1115 | |
| 1116 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1117 | BasicBlock *InsertAtEnd) { |
| 1118 | if (!Op->getType()->isFloatingPoint()) |
| 1119 | return new BinaryOperator(Instruction::Sub, |
| 1120 | Constant::getNullValue(Op->getType()), Op, |
| 1121 | Op->getType(), Name, InsertAtEnd); |
| 1122 | else |
| 1123 | return new BinaryOperator(Instruction::Sub, |
| 1124 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1125 | Op->getType(), Name, InsertAtEnd); |
| 1126 | } |
| 1127 | |
| 1128 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1129 | Instruction *InsertBefore) { |
Chris Lattner | fdbc82a | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1130 | Constant *C; |
| 1131 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1132 | C = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1133 | C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
| 1134 | } else { |
| 1135 | C = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1136 | } |
| 1137 | |
| 1138 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1139 | Op->getType(), Name, InsertBefore); |
| 1140 | } |
| 1141 | |
| 1142 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1143 | BasicBlock *InsertAtEnd) { |
Chris Lattner | b9d4100 | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1144 | Constant *AllOnes; |
| 1145 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1146 | // Create a vector of all ones values. |
| 1147 | Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1148 | AllOnes = |
| 1149 | ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
| 1150 | } else { |
| 1151 | AllOnes = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1152 | } |
| 1153 | |
| 1154 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1155 | Op->getType(), Name, InsertAtEnd); |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | // isConstantAllOnes - Helper function for several functions below |
| 1160 | static inline bool isConstantAllOnes(const Value *V) { |
| 1161 | return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue(); |
| 1162 | } |
| 1163 | |
| 1164 | bool BinaryOperator::isNeg(const Value *V) { |
| 1165 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1166 | if (Bop->getOpcode() == Instruction::Sub) |
| 1167 | if (!V->getType()->isFloatingPoint()) |
| 1168 | return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); |
| 1169 | else |
| 1170 | return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | bool BinaryOperator::isNot(const Value *V) { |
| 1175 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1176 | return (Bop->getOpcode() == Instruction::Xor && |
| 1177 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1178 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1182 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1183 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1184 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1187 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1188 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1191 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1192 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1193 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1194 | Value *Op0 = BO->getOperand(0); |
| 1195 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1196 | if (isConstantAllOnes(Op0)) return Op1; |
| 1197 | |
| 1198 | assert(isConstantAllOnes(Op1)); |
| 1199 | return Op0; |
| 1200 | } |
| 1201 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1202 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1203 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | // swapOperands - Exchange the two operands to this instruction. This |
| 1208 | // instruction is safe to use on any binary instruction and does not |
| 1209 | // modify the semantics of the instruction. If the instruction is |
| 1210 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1211 | // |
| 1212 | bool BinaryOperator::swapOperands() { |
| 1213 | if (isCommutative()) |
| 1214 | ; // If the instruction is commutative, it is safe to swap the operands |
| 1215 | else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) |
| 1216 | /// FIXME: SetCC instructions shouldn't all have different opcodes. |
| 1217 | setOpcode(SCI->getSwappedCondition()); |
| 1218 | else |
| 1219 | return true; // Can't commute operands |
| 1220 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1221 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
| 1224 | |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1225 | //===----------------------------------------------------------------------===// |
| 1226 | // CastInst Class |
| 1227 | //===----------------------------------------------------------------------===// |
| 1228 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 1229 | // Just determine if this cast only deals with integral->integral conversion. |
| 1230 | bool CastInst::isIntegerCast() const { |
| 1231 | switch (getOpcode()) { |
| 1232 | default: return false; |
| 1233 | case Instruction::ZExt: |
| 1234 | case Instruction::SExt: |
| 1235 | case Instruction::Trunc: |
| 1236 | return true; |
| 1237 | case Instruction::BitCast: |
| 1238 | return getOperand(0)->getType()->isIntegral() && getType()->isIntegral(); |
| 1239 | } |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 1242 | bool CastInst::isLosslessCast() const { |
| 1243 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1244 | if (getOpcode() != Instruction::BitCast) |
| 1245 | return false; |
| 1246 | |
| 1247 | // Identity cast is always lossless |
| 1248 | const Type* SrcTy = getOperand(0)->getType(); |
| 1249 | const Type* DstTy = getType(); |
| 1250 | if (SrcTy == DstTy) |
| 1251 | return true; |
| 1252 | |
| 1253 | // The remaining possibilities are lossless if the typeID of the source type |
| 1254 | // matches the type ID of the destination in size and fundamental type. This |
| 1255 | // prevents things like int -> ptr, int -> float, packed -> int, mismatched |
| 1256 | // packed types of the same size, and etc. |
| 1257 | switch (SrcTy->getTypeID()) { |
| 1258 | case Type::UByteTyID: return DstTy == Type::SByteTy; |
| 1259 | case Type::SByteTyID: return DstTy == Type::UByteTy; |
| 1260 | case Type::UShortTyID: return DstTy == Type::ShortTy; |
| 1261 | case Type::ShortTyID: return DstTy == Type::UShortTy; |
| 1262 | case Type::UIntTyID: return DstTy == Type::IntTy; |
| 1263 | case Type::IntTyID: return DstTy == Type::UIntTy; |
| 1264 | case Type::ULongTyID: return DstTy == Type::LongTy; |
| 1265 | case Type::LongTyID: return DstTy == Type::ULongTy; |
| 1266 | case Type::PointerTyID: return isa<PointerType>(DstTy); |
| 1267 | default: |
| 1268 | break; |
| 1269 | } |
| 1270 | return false; // Other types have no identity values |
| 1271 | } |
| 1272 | |
| 1273 | /// This function determines if the CastInst does not require any bits to be |
| 1274 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1275 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1276 | /// example, the following are all no-op casts: |
| 1277 | /// # bitcast uint %X, int |
| 1278 | /// # bitcast uint* %x, sbyte* |
| 1279 | /// # bitcast packed< 2 x int > %x, packed< 4 x short> |
| 1280 | /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only |
| 1281 | /// @brief Determine if a cast is a no-op. |
| 1282 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1283 | switch (getOpcode()) { |
| 1284 | default: |
| 1285 | assert(!"Invalid CastOp"); |
| 1286 | case Instruction::Trunc: |
| 1287 | case Instruction::ZExt: |
| 1288 | case Instruction::SExt: |
| 1289 | case Instruction::FPTrunc: |
| 1290 | case Instruction::FPExt: |
| 1291 | case Instruction::UIToFP: |
| 1292 | case Instruction::SIToFP: |
| 1293 | case Instruction::FPToUI: |
| 1294 | case Instruction::FPToSI: |
| 1295 | return false; // These always modify bits |
| 1296 | case Instruction::BitCast: |
| 1297 | return true; // BitCast never modifies bits. |
| 1298 | case Instruction::PtrToInt: |
| 1299 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1300 | getType()->getPrimitiveSizeInBits(); |
| 1301 | case Instruction::IntToPtr: |
| 1302 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1303 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | /// This function determines if a pair of casts can be eliminated and what |
| 1308 | /// opcode should be used in the elimination. This assumes that there are two |
| 1309 | /// instructions like this: |
| 1310 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1311 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1312 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1313 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1314 | /// If no such cast is permited, the function returns 0. |
| 1315 | unsigned CastInst::isEliminableCastPair( |
| 1316 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1317 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1318 | { |
| 1319 | // Define the 144 possibilities for these two cast instructions. The values |
| 1320 | // in this matrix determine what to do in a given situation and select the |
| 1321 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1322 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1323 | // the following cast properties: |
| 1324 | // |
| 1325 | // Size Compare Source Destination |
| 1326 | // Operator Src ? Size Type Sign Type Sign |
| 1327 | // -------- ------------ ------------------- --------------------- |
| 1328 | // TRUNC > Integer Any Integral Any |
| 1329 | // ZEXT < Integral Unsigned Integer Any |
| 1330 | // SEXT < Integral Signed Integer Any |
| 1331 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1332 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1333 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1334 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1335 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1336 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1337 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1338 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1339 | // BITCONVERT = FirstClass n/a FirstClass n/a |
| 1340 | // |
| 1341 | const unsigned numCastOps = |
| 1342 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1343 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1344 | // T F F U S F F P I B -+ |
| 1345 | // R Z S P P I I T P 2 N T | |
| 1346 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1347 | // N X X U S F F N X N 2 V | |
| 1348 | // C T T I I P P C T T P T -+ |
| 1349 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1350 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1351 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
| 1352 | { 0, 1, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1353 | { 0, 0, 1,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
| 1354 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1355 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1356 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1357 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1358 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1359 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1360 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1361 | }; |
| 1362 | |
| 1363 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1364 | [secondOp-Instruction::CastOpsBegin]; |
| 1365 | switch (ElimCase) { |
| 1366 | case 0: |
| 1367 | // categorically disallowed |
| 1368 | return 0; |
| 1369 | case 1: |
| 1370 | // allowed, use first cast's opcode |
| 1371 | return firstOp; |
| 1372 | case 2: |
| 1373 | // allowed, use second cast's opcode |
| 1374 | return secondOp; |
| 1375 | case 3: |
| 1376 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1377 | // is integer |
| 1378 | if (DstTy->isInteger()) |
| 1379 | return firstOp; |
| 1380 | return 0; |
| 1381 | case 4: |
| 1382 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1383 | // is floating point |
| 1384 | if (DstTy->isFloatingPoint()) |
| 1385 | return firstOp; |
| 1386 | return 0; |
| 1387 | case 5: |
| 1388 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1389 | // is an integer |
| 1390 | if (SrcTy->isInteger()) |
| 1391 | return secondOp; |
| 1392 | return 0; |
| 1393 | case 6: |
| 1394 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1395 | // is a floating point |
| 1396 | if (SrcTy->isFloatingPoint()) |
| 1397 | return secondOp; |
| 1398 | return 0; |
| 1399 | case 7: { |
| 1400 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1401 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1402 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1403 | if (MidSize >= PtrSize) |
| 1404 | return Instruction::BitCast; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | case 8: { |
| 1408 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1409 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1410 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1411 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1412 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1413 | if (SrcSize == DstSize) |
| 1414 | return Instruction::BitCast; |
| 1415 | else if (SrcSize < DstSize) |
| 1416 | return firstOp; |
| 1417 | return secondOp; |
| 1418 | } |
| 1419 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1420 | return Instruction::ZExt; |
| 1421 | case 10: |
| 1422 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1423 | // the same as the original, in which case its just a bitcast |
| 1424 | if (SrcTy == DstTy) |
| 1425 | return Instruction::BitCast; |
| 1426 | return 0; // If the types are not the same we can't eliminate it. |
| 1427 | case 11: |
| 1428 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1429 | // is a pointer to pointer cast. |
| 1430 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1431 | return secondOp; |
| 1432 | return 0; |
| 1433 | case 12: |
| 1434 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1435 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1436 | return firstOp; |
| 1437 | return 0; |
| 1438 | case 13: { |
| 1439 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1440 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1441 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1442 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1443 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1444 | return Instruction::BitCast; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | case 99: |
| 1448 | // cast combination can't happen (error in input). This is for all cases |
| 1449 | // where the MidTy is not the same for the two cast instructions. |
| 1450 | assert(!"Invalid Cast Combination"); |
| 1451 | return 0; |
| 1452 | default: |
| 1453 | assert(!"Error in CastResults table!!!"); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1460 | const std::string &Name, Instruction *InsertBefore) { |
| 1461 | // Construct and return the appropriate CastInst subclass |
| 1462 | switch (op) { |
| 1463 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1464 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1465 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1466 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1467 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1468 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1469 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1470 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1471 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1472 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1473 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1474 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1475 | default: |
| 1476 | assert(!"Invalid opcode provided"); |
| 1477 | } |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1482 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1483 | // Construct and return the appropriate CastInst subclass |
| 1484 | switch (op) { |
| 1485 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1486 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1487 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1488 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1489 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1490 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1491 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1492 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1493 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1494 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1495 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1496 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1497 | default: |
| 1498 | assert(!"Invalid opcode provided"); |
| 1499 | } |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 1504 | // types and size of the operand. This, basically, is a parallel of the |
| 1505 | // logic in the checkCast function below. This axiom should hold: |
| 1506 | // checkCast( getCastOpcode(Val, Ty), Val, Ty) |
| 1507 | // should not assert in checkCast. In other words, this produces a "correct" |
| 1508 | // casting opcode for the arguments passed to it. |
| 1509 | Instruction::CastOps |
| 1510 | CastInst::getCastOpcode(const Value *Src, const Type *DestTy) { |
| 1511 | // Get the bit sizes, we'll need these |
| 1512 | const Type *SrcTy = Src->getType(); |
| 1513 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1514 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1515 | |
| 1516 | // Run through the possibilities ... |
| 1517 | if (DestTy->isIntegral()) { // Casting to integral |
| 1518 | if (SrcTy->isIntegral()) { // Casting from integral |
| 1519 | if (DestBits < SrcBits) |
| 1520 | return Trunc; // int -> smaller int |
| 1521 | else if (DestBits > SrcBits) { // its an extension |
| 1522 | if (SrcTy->isSigned()) |
| 1523 | return SExt; // signed -> SEXT |
| 1524 | else |
| 1525 | return ZExt; // unsigned -> ZEXT |
| 1526 | } else { |
| 1527 | return BitCast; // Same size, No-op cast |
| 1528 | } |
| 1529 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1530 | if (DestTy->isSigned()) |
| 1531 | return FPToSI; // FP -> sint |
| 1532 | else |
| 1533 | return FPToUI; // FP -> uint |
| 1534 | } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { |
| 1535 | assert(DestBits == PTy->getBitWidth() && |
| 1536 | "Casting packed to integer of different width"); |
| 1537 | return BitCast; // Same size, no-op cast |
| 1538 | } else { |
| 1539 | assert(isa<PointerType>(SrcTy) && |
| 1540 | "Casting from a value that is not first-class type"); |
| 1541 | return PtrToInt; // ptr -> int |
| 1542 | } |
| 1543 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
| 1544 | if (SrcTy->isIntegral()) { // Casting from integral |
| 1545 | if (SrcTy->isSigned()) |
| 1546 | return SIToFP; // sint -> FP |
| 1547 | else |
| 1548 | return UIToFP; // uint -> FP |
| 1549 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1550 | if (DestBits < SrcBits) { |
| 1551 | return FPTrunc; // FP -> smaller FP |
| 1552 | } else if (DestBits > SrcBits) { |
| 1553 | return FPExt; // FP -> larger FP |
| 1554 | } else { |
| 1555 | return BitCast; // same size, no-op cast |
| 1556 | } |
| 1557 | } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { |
| 1558 | assert(DestBits == PTy->getBitWidth() && |
| 1559 | "Casting packed to floating point of different width"); |
| 1560 | return BitCast; // same size, no-op cast |
| 1561 | } else { |
| 1562 | assert(0 && "Casting pointer or non-first class to float"); |
| 1563 | } |
| 1564 | } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { |
| 1565 | if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) { |
| 1566 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
| 1567 | "Casting packed to packed of different widths"); |
| 1568 | return BitCast; // packed -> packed |
| 1569 | } else if (DestPTy->getBitWidth() == SrcBits) { |
| 1570 | return BitCast; // float/int -> packed |
| 1571 | } else { |
| 1572 | assert(!"Illegal cast to packed (wrong type or size)"); |
| 1573 | } |
| 1574 | } else if (isa<PointerType>(DestTy)) { |
| 1575 | if (isa<PointerType>(SrcTy)) { |
| 1576 | return BitCast; // ptr -> ptr |
| 1577 | } else if (SrcTy->isIntegral()) { |
| 1578 | return IntToPtr; // int -> ptr |
| 1579 | } else { |
| 1580 | assert(!"Casting pointer to other than pointer or int"); |
| 1581 | } |
| 1582 | } else { |
| 1583 | assert(!"Casting to type that is not first-class"); |
| 1584 | } |
| 1585 | |
| 1586 | // If we fall through to here we probably hit an assertion cast above |
| 1587 | // and assertions are not turned on. Anything we return is an error, so |
| 1588 | // BitCast is as good a choice as any. |
| 1589 | return BitCast; |
| 1590 | } |
| 1591 | |
| 1592 | //===----------------------------------------------------------------------===// |
| 1593 | // CastInst SubClass Constructors |
| 1594 | //===----------------------------------------------------------------------===// |
| 1595 | |
| 1596 | /// Check that the construction parameters for a CastInst are correct. This |
| 1597 | /// could be broken out into the separate constructors but it is useful to have |
| 1598 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 1599 | /// of the types involved. |
| 1600 | static bool |
| 1601 | checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { |
| 1602 | |
| 1603 | // Check for type sanity on the arguments |
| 1604 | const Type *SrcTy = S->getType(); |
| 1605 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 1606 | return false; |
| 1607 | |
| 1608 | // Get the size of the types in bits, we'll need this later |
| 1609 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1610 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1611 | |
| 1612 | // Switch on the opcode provided |
| 1613 | switch (op) { |
| 1614 | default: return false; // This is an input error |
| 1615 | case Instruction::Trunc: |
| 1616 | return SrcTy->isInteger() && DstTy->isIntegral() && SrcBitSize > DstBitSize; |
| 1617 | case Instruction::ZExt: |
| 1618 | return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; |
| 1619 | case Instruction::SExt: |
| 1620 | return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; |
| 1621 | case Instruction::FPTrunc: |
| 1622 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1623 | SrcBitSize > DstBitSize; |
| 1624 | case Instruction::FPExt: |
| 1625 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1626 | SrcBitSize < DstBitSize; |
| 1627 | case Instruction::UIToFP: |
| 1628 | return SrcTy->isIntegral() && DstTy->isFloatingPoint(); |
| 1629 | case Instruction::SIToFP: |
| 1630 | return SrcTy->isIntegral() && DstTy->isFloatingPoint(); |
| 1631 | case Instruction::FPToUI: |
| 1632 | return SrcTy->isFloatingPoint() && DstTy->isIntegral(); |
| 1633 | case Instruction::FPToSI: |
| 1634 | return SrcTy->isFloatingPoint() && DstTy->isIntegral(); |
| 1635 | case Instruction::PtrToInt: |
| 1636 | return isa<PointerType>(SrcTy) && DstTy->isIntegral(); |
| 1637 | case Instruction::IntToPtr: |
| 1638 | return SrcTy->isIntegral() && isa<PointerType>(DstTy); |
| 1639 | case Instruction::BitCast: |
| 1640 | // BitCast implies a no-op cast of type only. No bits change. |
| 1641 | // However, you can't cast pointers to anything but pointers. |
| 1642 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 1643 | return false; |
| 1644 | |
| 1645 | // Now we know we're not dealing with a pointer/non-poiner mismatch. In all |
| 1646 | // these cases, the cast is okay if the source and destination bit widths |
| 1647 | // are identical. |
| 1648 | return SrcBitSize == DstBitSize; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | TruncInst::TruncInst( |
| 1653 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1654 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
| 1655 | assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); |
| 1656 | } |
| 1657 | |
| 1658 | TruncInst::TruncInst( |
| 1659 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1660 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
| 1661 | assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); |
| 1662 | } |
| 1663 | |
| 1664 | ZExtInst::ZExtInst( |
| 1665 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1666 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
| 1667 | assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); |
| 1668 | } |
| 1669 | |
| 1670 | ZExtInst::ZExtInst( |
| 1671 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1672 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
| 1673 | assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); |
| 1674 | } |
| 1675 | SExtInst::SExtInst( |
| 1676 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1677 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
| 1678 | assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); |
| 1679 | } |
| 1680 | |
| 1681 | SExtInst::SExtInst::SExtInst( |
| 1682 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1683 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
| 1684 | assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); |
| 1685 | } |
| 1686 | |
| 1687 | FPTruncInst::FPTruncInst( |
| 1688 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1689 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
| 1690 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
| 1691 | } |
| 1692 | |
| 1693 | FPTruncInst::FPTruncInst( |
| 1694 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1695 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
| 1696 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
| 1697 | } |
| 1698 | |
| 1699 | FPExtInst::FPExtInst( |
| 1700 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1701 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
| 1702 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); |
| 1703 | } |
| 1704 | |
| 1705 | FPExtInst::FPExtInst( |
| 1706 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1707 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
| 1708 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); |
| 1709 | } |
| 1710 | |
| 1711 | UIToFPInst::UIToFPInst( |
| 1712 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1713 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
| 1714 | assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); |
| 1715 | } |
| 1716 | |
| 1717 | UIToFPInst::UIToFPInst( |
| 1718 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1719 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
| 1720 | assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); |
| 1721 | } |
| 1722 | |
| 1723 | SIToFPInst::SIToFPInst( |
| 1724 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1725 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
| 1726 | assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); |
| 1727 | } |
| 1728 | |
| 1729 | SIToFPInst::SIToFPInst( |
| 1730 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1731 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
| 1732 | assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); |
| 1733 | } |
| 1734 | |
| 1735 | FPToUIInst::FPToUIInst( |
| 1736 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1737 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
| 1738 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); |
| 1739 | } |
| 1740 | |
| 1741 | FPToUIInst::FPToUIInst( |
| 1742 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1743 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
| 1744 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); |
| 1745 | } |
| 1746 | |
| 1747 | FPToSIInst::FPToSIInst( |
| 1748 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1749 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
| 1750 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); |
| 1751 | } |
| 1752 | |
| 1753 | FPToSIInst::FPToSIInst( |
| 1754 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1755 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
| 1756 | assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); |
| 1757 | } |
| 1758 | |
| 1759 | PtrToIntInst::PtrToIntInst( |
| 1760 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1761 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
| 1762 | assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
| 1763 | } |
| 1764 | |
| 1765 | PtrToIntInst::PtrToIntInst( |
| 1766 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1767 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
| 1768 | assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
| 1769 | } |
| 1770 | |
| 1771 | IntToPtrInst::IntToPtrInst( |
| 1772 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1773 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
| 1774 | assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
| 1775 | } |
| 1776 | |
| 1777 | IntToPtrInst::IntToPtrInst( |
| 1778 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1779 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
| 1780 | assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
| 1781 | } |
| 1782 | |
| 1783 | BitCastInst::BitCastInst( |
| 1784 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1785 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
| 1786 | assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); |
| 1787 | } |
| 1788 | |
| 1789 | BitCastInst::BitCastInst( |
| 1790 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1791 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
| 1792 | assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); |
| 1793 | } |
Chris Lattner | 2f46386 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 1794 | |
| 1795 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1796 | // SetCondInst Class |
| 1797 | //===----------------------------------------------------------------------===// |
| 1798 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1799 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1800 | const std::string &Name, Instruction *InsertBefore) |
| 1801 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) { |
| 1802 | |
| 1803 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1804 | assert(getInverseCondition(Opcode)); |
| 1805 | } |
| 1806 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1807 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1808 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 1809 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) { |
| 1810 | |
| 1811 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1812 | assert(getInverseCondition(Opcode)); |
| 1813 | } |
| 1814 | |
| 1815 | // getInverseCondition - Return the inverse of the current condition opcode. |
| 1816 | // For example seteq -> setne, setgt -> setle, setlt -> setge, etc... |
| 1817 | // |
| 1818 | Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) { |
| 1819 | switch (Opcode) { |
| 1820 | default: |
| 1821 | assert(0 && "Unknown setcc opcode!"); |
| 1822 | case SetEQ: return SetNE; |
| 1823 | case SetNE: return SetEQ; |
| 1824 | case SetGT: return SetLE; |
| 1825 | case SetLT: return SetGE; |
| 1826 | case SetGE: return SetLT; |
| 1827 | case SetLE: return SetGT; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | // getSwappedCondition - Return the condition opcode that would be the result |
| 1832 | // of exchanging the two operands of the setcc instruction without changing |
| 1833 | // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. |
| 1834 | // |
| 1835 | Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) { |
| 1836 | switch (Opcode) { |
| 1837 | default: assert(0 && "Unknown setcc instruction!"); |
| 1838 | case SetEQ: case SetNE: return Opcode; |
| 1839 | case SetGT: return SetLT; |
| 1840 | case SetLT: return SetGT; |
| 1841 | case SetGE: return SetLE; |
| 1842 | case SetLE: return SetGE; |
| 1843 | } |
| 1844 | } |
| 1845 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1846 | |
| 1847 | //===----------------------------------------------------------------------===// |
| 1848 | // CmpInst Classes |
| 1849 | //===----------------------------------------------------------------------===// |
| 1850 | |
| 1851 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1852 | const std::string &Name, Instruction *InsertBefore) |
| 1853 | : Instruction(Type::BoolTy, op, Ops, 2, Name, InsertBefore) { |
| 1854 | Ops[0].init(LHS, this); |
| 1855 | Ops[1].init(RHS, this); |
| 1856 | SubclassData = predicate; |
| 1857 | if (op == Instruction::ICmp) { |
| 1858 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1859 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1860 | "Invalid ICmp predicate value"); |
| 1861 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1862 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1863 | assert(Op0Ty == Op1Ty && |
| 1864 | "Both operands to ICmp instruction are not of the same type!"); |
| 1865 | // Check that the operands are the right type |
| 1866 | assert(Op0Ty->isIntegral() || Op0Ty->getTypeID() == Type::PointerTyID || |
| 1867 | (isa<PackedType>(Op0Ty) && |
| 1868 | cast<PackedType>(Op0Ty)->getElementType()->isIntegral()) && |
| 1869 | "Invalid operand types for ICmp instruction"); |
| 1870 | return; |
| 1871 | } |
| 1872 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 1873 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 1874 | "Invalid FCmp predicate value"); |
| 1875 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1876 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1877 | assert(Op0Ty == Op1Ty && |
| 1878 | "Both operands to FCmp instruction are not of the same type!"); |
| 1879 | // Check that the operands are the right type |
| 1880 | assert(Op0Ty->isFloatingPoint() || (isa<PackedType>(Op0Ty) && |
| 1881 | cast<PackedType>(Op0Ty)->getElementType()->isFloatingPoint()) && |
| 1882 | "Invalid operand types for FCmp instruction"); |
| 1883 | } |
| 1884 | |
| 1885 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1886 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 1887 | : Instruction(Type::BoolTy, op, Ops, 2, Name, InsertAtEnd) { |
| 1888 | Ops[0].init(LHS, this); |
| 1889 | Ops[1].init(RHS, this); |
| 1890 | SubclassData = predicate; |
| 1891 | if (op == Instruction::ICmp) { |
| 1892 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1893 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1894 | "Invalid ICmp predicate value"); |
| 1895 | |
| 1896 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1897 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1898 | assert(Op0Ty == Op1Ty && |
| 1899 | "Both operands to ICmp instruction are not of the same type!"); |
| 1900 | // Check that the operands are the right type |
| 1901 | assert(Op0Ty->isIntegral() || Op0Ty->getTypeID() == Type::PointerTyID || |
| 1902 | (isa<PackedType>(Op0Ty) && |
| 1903 | cast<PackedType>(Op0Ty)->getElementType()->isIntegral()) && |
| 1904 | "Invalid operand types for ICmp instruction"); |
| 1905 | return; |
| 1906 | } |
| 1907 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 1908 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 1909 | "Invalid FCmp predicate value"); |
| 1910 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1911 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1912 | assert(Op0Ty == Op1Ty && |
| 1913 | "Both operands to FCmp instruction are not of the same type!"); |
| 1914 | // Check that the operands are the right type |
| 1915 | assert(Op0Ty->isFloatingPoint() || (isa<PackedType>(Op0Ty) && |
| 1916 | cast<PackedType>(Op0Ty)->getElementType()->isFloatingPoint()) && |
| 1917 | "Invalid operand types for FCmp instruction"); |
| 1918 | } |
| 1919 | |
| 1920 | CmpInst * |
| 1921 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 1922 | const std::string &Name, Instruction *InsertBefore) { |
| 1923 | if (Op == Instruction::ICmp) { |
| 1924 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 1925 | InsertBefore); |
| 1926 | } |
| 1927 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 1928 | InsertBefore); |
| 1929 | } |
| 1930 | |
| 1931 | CmpInst * |
| 1932 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 1933 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1934 | if (Op == Instruction::ICmp) { |
| 1935 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 1936 | InsertAtEnd); |
| 1937 | } |
| 1938 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 1939 | InsertAtEnd); |
| 1940 | } |
| 1941 | |
| 1942 | void CmpInst::swapOperands() { |
| 1943 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 1944 | IC->swapOperands(); |
| 1945 | else |
| 1946 | cast<FCmpInst>(this)->swapOperands(); |
| 1947 | } |
| 1948 | |
| 1949 | bool CmpInst::isCommutative() { |
| 1950 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 1951 | return IC->isCommutative(); |
| 1952 | return cast<FCmpInst>(this)->isCommutative(); |
| 1953 | } |
| 1954 | |
| 1955 | bool CmpInst::isEquality() { |
| 1956 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 1957 | return IC->isEquality(); |
| 1958 | return cast<FCmpInst>(this)->isEquality(); |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { |
| 1963 | switch (pred) { |
| 1964 | default: |
| 1965 | assert(!"Unknown icmp predicate!"); |
| 1966 | case ICMP_EQ: return ICMP_NE; |
| 1967 | case ICMP_NE: return ICMP_EQ; |
| 1968 | case ICMP_UGT: return ICMP_ULE; |
| 1969 | case ICMP_ULT: return ICMP_UGE; |
| 1970 | case ICMP_UGE: return ICMP_ULT; |
| 1971 | case ICMP_ULE: return ICMP_UGT; |
| 1972 | case ICMP_SGT: return ICMP_SLE; |
| 1973 | case ICMP_SLT: return ICMP_SGE; |
| 1974 | case ICMP_SGE: return ICMP_SLT; |
| 1975 | case ICMP_SLE: return ICMP_SGT; |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { |
| 1980 | switch (pred) { |
| 1981 | default: assert(! "Unknown setcc instruction!"); |
| 1982 | case ICMP_EQ: case ICMP_NE: |
| 1983 | return pred; |
| 1984 | case ICMP_SGT: return ICMP_SLT; |
| 1985 | case ICMP_SLT: return ICMP_SGT; |
| 1986 | case ICMP_SGE: return ICMP_SLE; |
| 1987 | case ICMP_SLE: return ICMP_SGE; |
| 1988 | case ICMP_UGT: return ICMP_ULT; |
| 1989 | case ICMP_ULT: return ICMP_UGT; |
| 1990 | case ICMP_UGE: return ICMP_ULE; |
| 1991 | case ICMP_ULE: return ICMP_UGE; |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { |
| 1996 | switch (pred) { |
| 1997 | default: |
| 1998 | assert(!"Unknown icmp predicate!"); |
| 1999 | case FCMP_OEQ: return FCMP_UNE; |
| 2000 | case FCMP_ONE: return FCMP_UEQ; |
| 2001 | case FCMP_OGT: return FCMP_ULE; |
| 2002 | case FCMP_OLT: return FCMP_UGE; |
| 2003 | case FCMP_OGE: return FCMP_ULT; |
| 2004 | case FCMP_OLE: return FCMP_UGT; |
| 2005 | case FCMP_UEQ: return FCMP_ONE; |
| 2006 | case FCMP_UNE: return FCMP_OEQ; |
| 2007 | case FCMP_UGT: return FCMP_OLE; |
| 2008 | case FCMP_ULT: return FCMP_OGE; |
| 2009 | case FCMP_UGE: return FCMP_OLT; |
| 2010 | case FCMP_ULE: return FCMP_OGT; |
| 2011 | case FCMP_ORD: return FCMP_UNO; |
| 2012 | case FCMP_UNO: return FCMP_ORD; |
| 2013 | case FCMP_TRUE: return FCMP_FALSE; |
| 2014 | case FCMP_FALSE: return FCMP_TRUE; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { |
| 2019 | switch (pred) { |
| 2020 | default: assert(!"Unknown setcc instruction!"); |
| 2021 | case FCMP_FALSE: case FCMP_TRUE: |
| 2022 | case FCMP_OEQ: case FCMP_ONE: |
| 2023 | case FCMP_UEQ: case FCMP_UNE: |
| 2024 | case FCMP_ORD: case FCMP_UNO: |
| 2025 | return pred; |
| 2026 | case FCMP_OGT: return FCMP_OLT; |
| 2027 | case FCMP_OLT: return FCMP_OGT; |
| 2028 | case FCMP_OGE: return FCMP_OLE; |
| 2029 | case FCMP_OLE: return FCMP_OGE; |
| 2030 | case FCMP_UGT: return FCMP_ULT; |
| 2031 | case FCMP_ULT: return FCMP_UGT; |
| 2032 | case FCMP_UGE: return FCMP_ULE; |
| 2033 | case FCMP_ULE: return FCMP_UGE; |
| 2034 | } |
| 2035 | } |
| 2036 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2037 | //===----------------------------------------------------------------------===// |
| 2038 | // SwitchInst Implementation |
| 2039 | //===----------------------------------------------------------------------===// |
| 2040 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2041 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2042 | assert(Value && Default); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2043 | ReservedSpace = 2+NumCases*2; |
| 2044 | NumOperands = 2; |
| 2045 | OperandList = new Use[ReservedSpace]; |
| 2046 | |
| 2047 | OperandList[0].init(Value, this); |
| 2048 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2051 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2052 | : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], |
| 2053 | SI.getNumOperands()) { |
| 2054 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2055 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 2056 | OL[i].init(InOL[i], this); |
| 2057 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2061 | SwitchInst::~SwitchInst() { |
| 2062 | delete [] OperandList; |
| 2063 | } |
| 2064 | |
| 2065 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2066 | /// addCase - Add an entry to the switch instruction... |
| 2067 | /// |
Chris Lattner | d1a3260 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2068 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2069 | unsigned OpNo = NumOperands; |
| 2070 | if (OpNo+2 > ReservedSpace) |
| 2071 | resizeOperands(0); // Get more space! |
| 2072 | // Initialize some new operands. |
Chris Lattner | 1f377fc | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2073 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2074 | NumOperands = OpNo+2; |
| 2075 | OperandList[OpNo].init(OnVal, this); |
| 2076 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /// removeCase - This method removes the specified successor from the switch |
| 2080 | /// instruction. Note that this cannot be used to remove the default |
| 2081 | /// destination (successor #0). |
| 2082 | /// |
| 2083 | void SwitchInst::removeCase(unsigned idx) { |
| 2084 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2085 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2086 | |
| 2087 | unsigned NumOps = getNumOperands(); |
| 2088 | Use *OL = OperandList; |
| 2089 | |
| 2090 | // Move everything after this operand down. |
| 2091 | // |
| 2092 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2093 | // client might not expect this to happen. The code as it is thrashes the |
| 2094 | // use/def lists, which is kinda lame. |
| 2095 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2096 | OL[i-2] = OL[i]; |
| 2097 | OL[i-2+1] = OL[i+1]; |
| 2098 | } |
| 2099 | |
| 2100 | // Nuke the last value. |
| 2101 | OL[NumOps-2].set(0); |
| 2102 | OL[NumOps-2+1].set(0); |
| 2103 | NumOperands = NumOps-2; |
| 2104 | } |
| 2105 | |
| 2106 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2107 | /// list according to the following behavior: |
| 2108 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 2109 | /// of operation. This grows the number of ops by 1.5 times. |
| 2110 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2111 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2112 | /// |
| 2113 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 2114 | if (NumOps == 0) { |
Chris Lattner | 1f377fc | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2115 | NumOps = getNumOperands()/2*6; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2116 | } else if (NumOps*2 > NumOperands) { |
| 2117 | // No resize needed. |
| 2118 | if (ReservedSpace >= NumOps) return; |
| 2119 | } else if (NumOps == NumOperands) { |
| 2120 | if (ReservedSpace == NumOps) return; |
| 2121 | } else { |
Chris Lattner | 1f377fc | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2122 | return; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | ReservedSpace = NumOps; |
| 2126 | Use *NewOps = new Use[NumOps]; |
| 2127 | Use *OldOps = OperandList; |
| 2128 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2129 | NewOps[i].init(OldOps[i], this); |
| 2130 | OldOps[i].set(0); |
| 2131 | } |
| 2132 | delete [] OldOps; |
| 2133 | OperandList = NewOps; |
| 2134 | } |
| 2135 | |
| 2136 | |
| 2137 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2138 | return getSuccessor(idx); |
| 2139 | } |
| 2140 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2141 | return getNumSuccessors(); |
| 2142 | } |
| 2143 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2144 | setSuccessor(idx, B); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2145 | } |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2146 | |
| 2147 | |
| 2148 | // Define these methods here so vtables don't get emitted into every translation |
| 2149 | // unit that uses these classes. |
| 2150 | |
| 2151 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 2152 | return new GetElementPtrInst(*this); |
| 2153 | } |
| 2154 | |
| 2155 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2156 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2159 | CmpInst* CmpInst::clone() const { |
| 2160 | return create(Instruction::OtherOps(getOpcode()), getPredicate(), |
| 2161 | Ops[0], Ops[1]); |
| 2162 | } |
| 2163 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 2164 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2165 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2166 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2167 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2168 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2169 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2170 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2171 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2172 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2173 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2174 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2175 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2176 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2177 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2178 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2179 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2180 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
| 2181 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
| 2182 | ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } |
| 2183 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
| 2184 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2185 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2186 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2187 | return new ExtractElementInst(*this); |
| 2188 | } |
| 2189 | InsertElementInst *InsertElementInst::clone() const { |
| 2190 | return new InsertElementInst(*this); |
| 2191 | } |
| 2192 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2193 | return new ShuffleVectorInst(*this); |
| 2194 | } |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2195 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 2196 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 2197 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 2198 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 2199 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 2200 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2201 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |