Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 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 | 93a7c06 | 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 | f7b6d31 | 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 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 38 | // TerminatorInst Class |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | Use *Ops, unsigned NumOps, Instruction *IB) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 43 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) { |
| 44 | } |
| 45 | |
| 46 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
| 47 | Use *Ops, unsigned NumOps, BasicBlock *IAE) |
| 48 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) { |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // PHINode Class |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
| 57 | PHINode::PHINode(const PHINode &PN) |
| 58 | : Instruction(PN.getType(), Instruction::PHI, |
| 59 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 60 | ReservedSpace(PN.getNumOperands()) { |
| 61 | Use *OL = OperandList; |
| 62 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 63 | OL[i].init(PN.getOperand(i), this); |
| 64 | OL[i+1].init(PN.getOperand(i+1), this); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | PHINode::~PHINode() { |
| 69 | delete [] OperandList; |
| 70 | } |
| 71 | |
| 72 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 73 | // predecessor basic block is deleted. |
| 74 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 75 | unsigned NumOps = getNumOperands(); |
| 76 | Use *OL = OperandList; |
| 77 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 78 | Value *Removed = OL[Idx*2]; |
| 79 | |
| 80 | // Move everything after this operand down. |
| 81 | // |
| 82 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 83 | // client might not expect this to happen. The code as it is thrashes the |
| 84 | // use/def lists, which is kinda lame. |
| 85 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 86 | OL[i-2] = OL[i]; |
| 87 | OL[i-2+1] = OL[i+1]; |
| 88 | } |
| 89 | |
| 90 | // Nuke the last value. |
| 91 | OL[NumOps-2].set(0); |
| 92 | OL[NumOps-2+1].set(0); |
| 93 | NumOperands = NumOps-2; |
| 94 | |
| 95 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 96 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 97 | // If anyone is using this PHI, make them use a dummy value instead... |
| 98 | replaceAllUsesWith(UndefValue::get(getType())); |
| 99 | eraseFromParent(); |
| 100 | } |
| 101 | return Removed; |
| 102 | } |
| 103 | |
| 104 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 105 | /// list according to the following behavior: |
| 106 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 107 | /// of operation. This grows the number of ops by 1.5 times. |
| 108 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 109 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 110 | /// |
| 111 | void PHINode::resizeOperands(unsigned NumOps) { |
| 112 | if (NumOps == 0) { |
| 113 | NumOps = (getNumOperands())*3/2; |
| 114 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 115 | } else if (NumOps*2 > NumOperands) { |
| 116 | // No resize needed. |
| 117 | if (ReservedSpace >= NumOps) return; |
| 118 | } else if (NumOps == NumOperands) { |
| 119 | if (ReservedSpace == NumOps) return; |
| 120 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 121 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ReservedSpace = NumOps; |
| 125 | Use *NewOps = new Use[NumOps]; |
| 126 | Use *OldOps = OperandList; |
| 127 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 128 | NewOps[i].init(OldOps[i], this); |
| 129 | OldOps[i].set(0); |
| 130 | } |
| 131 | delete [] OldOps; |
| 132 | OperandList = NewOps; |
| 133 | } |
| 134 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 135 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 136 | /// value, return the value, otherwise return null. |
| 137 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 138 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 139 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 140 | if (getNumIncomingValues() == 1) |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 141 | if (getIncomingValue(0) != this) // not X = phi X |
| 142 | return getIncomingValue(0); |
| 143 | else |
| 144 | return UndefValue::get(getType()); // Self cycle is dead. |
| 145 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 146 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 147 | // the PHI node with the incoming value. |
| 148 | // |
| 149 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 150 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 151 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 152 | if (isa<UndefValue>(getIncomingValue(i))) |
| 153 | HasUndefInput = true; |
| 154 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 155 | if (InVal && getIncomingValue(i) != InVal) |
| 156 | return 0; // Not the same, bail out. |
| 157 | else |
| 158 | InVal = getIncomingValue(i); |
| 159 | |
| 160 | // The only case that could cause InVal to be null is if we have a PHI node |
| 161 | // that only has entries for itself. In this case, there is no entry into the |
| 162 | // loop, so kill the PHI. |
| 163 | // |
| 164 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 165 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 166 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 167 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 168 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 169 | // or if the client is prepared to deal with this possibility. |
| 170 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 171 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 172 | // If it's in the entry block, it dominates everything. |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 173 | if (IV->getParent() != &IV->getParent()->getParent()->front() || |
| 174 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 175 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 176 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 177 | // All of the incoming values are the same, return the value now. |
| 178 | return InVal; |
| 179 | } |
| 180 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 181 | |
| 182 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 183 | // CallInst Implementation |
| 184 | //===----------------------------------------------------------------------===// |
| 185 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 186 | CallInst::~CallInst() { |
| 187 | delete [] OperandList; |
| 188 | } |
| 189 | |
| 190 | void CallInst::init(Value *Func, const std::vector<Value*> &Params) { |
| 191 | NumOperands = Params.size()+1; |
| 192 | Use *OL = OperandList = new Use[Params.size()+1]; |
| 193 | OL[0].init(Func, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 194 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 195 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 196 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 197 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 198 | assert((Params.size() == FTy->getNumParams() || |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 199 | (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 200 | "Calling a function with bad signature!"); |
| 201 | for (unsigned i = 0, e = Params.size(); i != e; ++i) { |
| 202 | assert((i >= FTy->getNumParams() || |
| 203 | FTy->getParamType(i) == Params[i]->getType()) && |
| 204 | "Calling a function with a bad signature!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 205 | OL[i+1].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 206 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 209 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
| 210 | NumOperands = 3; |
| 211 | Use *OL = OperandList = new Use[3]; |
| 212 | OL[0].init(Func, this); |
| 213 | OL[1].init(Actual1, this); |
| 214 | OL[2].init(Actual2, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 215 | |
| 216 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 217 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 218 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 219 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 220 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 221 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 222 | assert((0 >= FTy->getNumParams() || |
| 223 | FTy->getParamType(0) == Actual1->getType()) && |
| 224 | "Calling a function with a bad signature!"); |
| 225 | assert((1 >= FTy->getNumParams() || |
| 226 | FTy->getParamType(1) == Actual2->getType()) && |
| 227 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 230 | void CallInst::init(Value *Func, Value *Actual) { |
| 231 | NumOperands = 2; |
| 232 | Use *OL = OperandList = new Use[2]; |
| 233 | OL[0].init(Func, this); |
| 234 | OL[1].init(Actual, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 235 | |
| 236 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 237 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 238 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 239 | assert((FTy->getNumParams() == 1 || |
| 240 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 241 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 242 | assert((0 == FTy->getNumParams() || |
| 243 | FTy->getParamType(0) == Actual->getType()) && |
| 244 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 247 | void CallInst::init(Value *Func) { |
| 248 | NumOperands = 1; |
| 249 | Use *OL = OperandList = new Use[1]; |
| 250 | OL[0].init(Func, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 251 | |
| 252 | const FunctionType *MTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 253 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 254 | |
| 255 | assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); |
| 256 | } |
| 257 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 258 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 259 | const std::string &Name, Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 260 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 261 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 262 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 263 | init(Func, Params); |
| 264 | } |
| 265 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 266 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 267 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 268 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 269 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 270 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 271 | init(Func, Params); |
| 272 | } |
| 273 | |
| 274 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 275 | const std::string &Name, Instruction *InsertBefore) |
| 276 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 277 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 278 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 279 | init(Func, Actual1, Actual2); |
| 280 | } |
| 281 | |
| 282 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 283 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 284 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 285 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 286 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 287 | init(Func, Actual1, Actual2); |
| 288 | } |
| 289 | |
| 290 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 291 | Instruction *InsertBefore) |
| 292 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 293 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 294 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 295 | init(Func, Actual); |
| 296 | } |
| 297 | |
| 298 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 299 | BasicBlock *InsertAtEnd) |
| 300 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 301 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 302 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 303 | init(Func, Actual); |
| 304 | } |
| 305 | |
| 306 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 307 | Instruction *InsertBefore) |
| 308 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 309 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 310 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 311 | init(Func); |
| 312 | } |
| 313 | |
| 314 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 315 | BasicBlock *InsertAtEnd) |
| 316 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 317 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 318 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 319 | init(Func); |
| 320 | } |
| 321 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 322 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 323 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
| 324 | CI.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 325 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 326 | Use *OL = OperandList; |
| 327 | Use *InOL = CI.OperandList; |
| 328 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 329 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 332 | |
| 333 | //===----------------------------------------------------------------------===// |
| 334 | // InvokeInst Implementation |
| 335 | //===----------------------------------------------------------------------===// |
| 336 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 337 | InvokeInst::~InvokeInst() { |
| 338 | delete [] OperandList; |
| 339 | } |
| 340 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 342 | const std::vector<Value*> &Params) { |
| 343 | NumOperands = 3+Params.size(); |
| 344 | Use *OL = OperandList = new Use[3+Params.size()]; |
| 345 | OL[0].init(Fn, this); |
| 346 | OL[1].init(IfNormal, this); |
| 347 | OL[2].init(IfException, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 348 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 349 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 350 | |
| 351 | assert((Params.size() == FTy->getNumParams()) || |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 352 | (FTy->isVarArg() && Params.size() > FTy->getNumParams()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 353 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 355 | for (unsigned i = 0, e = Params.size(); i != e; i++) { |
| 356 | assert((i >= FTy->getNumParams() || |
| 357 | FTy->getParamType(i) == Params[i]->getType()) && |
| 358 | "Invoking a function with a bad signature!"); |
| 359 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 360 | OL[i+3].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 361 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 365 | BasicBlock *IfException, |
| 366 | const std::vector<Value*> &Params, |
| 367 | const std::string &Name, Instruction *InsertBefore) |
| 368 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 369 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 370 | Instruction::Invoke, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 371 | init(Fn, IfNormal, IfException, Params); |
| 372 | } |
| 373 | |
| 374 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 375 | BasicBlock *IfException, |
| 376 | const std::vector<Value*> &Params, |
| 377 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 378 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 379 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 380 | Instruction::Invoke, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 381 | init(Fn, IfNormal, IfException, Params); |
| 382 | } |
| 383 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 384 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 385 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 386 | new Use[II.getNumOperands()], II.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 387 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 388 | Use *OL = OperandList, *InOL = II.OperandList; |
| 389 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 390 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 393 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 394 | return getSuccessor(idx); |
| 395 | } |
| 396 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 397 | return getNumSuccessors(); |
| 398 | } |
| 399 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 400 | return setSuccessor(idx, B); |
| 401 | } |
| 402 | |
| 403 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// |
| 405 | // ReturnInst Implementation |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 408 | void ReturnInst::init(Value *retVal) { |
| 409 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 410 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 411 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 412 | NumOperands = 1; |
| 413 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 417 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 418 | return getNumSuccessors(); |
| 419 | } |
| 420 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 421 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 422 | // emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 423 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 424 | assert(0 && "ReturnInst has no successors!"); |
| 425 | } |
| 426 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 427 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 428 | assert(0 && "ReturnInst has no successors!"); |
| 429 | abort(); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 434 | //===----------------------------------------------------------------------===// |
| 435 | // UnwindInst Implementation |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 438 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 439 | return getNumSuccessors(); |
| 440 | } |
| 441 | |
| 442 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 443 | assert(0 && "UnwindInst has no successors!"); |
| 444 | } |
| 445 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 446 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 447 | assert(0 && "UnwindInst has no successors!"); |
| 448 | abort(); |
| 449 | return 0; |
| 450 | } |
| 451 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 453 | // UnreachableInst Implementation |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 456 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 457 | return getNumSuccessors(); |
| 458 | } |
| 459 | |
| 460 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 461 | assert(0 && "UnwindInst has no successors!"); |
| 462 | } |
| 463 | |
| 464 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 465 | assert(0 && "UnwindInst has no successors!"); |
| 466 | abort(); |
| 467 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 471 | // BranchInst Implementation |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 474 | void BranchInst::AssertOK() { |
| 475 | if (isConditional()) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 476 | assert(getCondition()->getType() == Type::BoolTy && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 477 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 480 | BranchInst::BranchInst(const BranchInst &BI) : |
| 481 | TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) { |
| 482 | OperandList[0].init(BI.getOperand(0), this); |
| 483 | if (BI.getNumOperands() != 1) { |
| 484 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 485 | OperandList[1].init(BI.getOperand(1), this); |
| 486 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 490 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 491 | return getSuccessor(idx); |
| 492 | } |
| 493 | unsigned BranchInst::getNumSuccessorsV() const { |
| 494 | return getNumSuccessors(); |
| 495 | } |
| 496 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 497 | setSuccessor(idx, B); |
| 498 | } |
| 499 | |
| 500 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 501 | //===----------------------------------------------------------------------===// |
| 502 | // AllocationInst Implementation |
| 503 | //===----------------------------------------------------------------------===// |
| 504 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 505 | static Value *getAISize(Value *Amt) { |
| 506 | if (!Amt) |
| 507 | Amt = ConstantUInt::get(Type::UIntTy, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 508 | else { |
| 509 | assert(!isa<BasicBlock>(Amt) && |
| 510 | "Passed basic block into allocation size parameter! Ue other ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 511 | assert(Amt->getType() == Type::UIntTy && |
| 512 | "Malloc/Allocation array size != UIntTy!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 513 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 514 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 517 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 518 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 519 | Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 520 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 521 | Name, InsertBefore), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 522 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 523 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 526 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 527 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 528 | BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 529 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 530 | Name, InsertAtEnd), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 531 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 532 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | bool AllocationInst::isArrayAllocation() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 536 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0))) |
| 537 | return CUI->getValue() != 1; |
| 538 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | const Type *AllocationInst::getAllocatedType() const { |
| 542 | return getType()->getElementType(); |
| 543 | } |
| 544 | |
| 545 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 546 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 547 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | MallocInst::MallocInst(const MallocInst &MI) |
| 551 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 552 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | // FreeInst Implementation |
| 557 | //===----------------------------------------------------------------------===// |
| 558 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 559 | void FreeInst::AssertOK() { |
| 560 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 561 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 565 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) { |
| 566 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 570 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) { |
| 571 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
| 575 | //===----------------------------------------------------------------------===// |
| 576 | // LoadInst Implementation |
| 577 | //===----------------------------------------------------------------------===// |
| 578 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 579 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 580 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 581 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 585 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 586 | Load, Ptr, Name, InsertBef) { |
| 587 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 588 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 592 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 593 | Load, Ptr, Name, InsertAE) { |
| 594 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 595 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 599 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 600 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 601 | Load, Ptr, Name, InsertBef) { |
| 602 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 603 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 607 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 608 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 609 | Load, Ptr, Name, InsertAE) { |
| 610 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 611 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | //===----------------------------------------------------------------------===// |
| 616 | // StoreInst Implementation |
| 617 | //===----------------------------------------------------------------------===// |
| 618 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 619 | void StoreInst::AssertOK() { |
| 620 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 621 | "Ptr must have pointer type!"); |
| 622 | assert(getOperand(0)->getType() == |
| 623 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 624 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 625 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 626 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 627 | |
| 628 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 629 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 630 | Ops[0].init(val, this); |
| 631 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 632 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 633 | AssertOK(); |
| 634 | } |
| 635 | |
| 636 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 637 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 638 | Ops[0].init(val, this); |
| 639 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 640 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 641 | AssertOK(); |
| 642 | } |
| 643 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 644 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 645 | Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 646 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 647 | Ops[0].init(val, this); |
| 648 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 649 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 650 | AssertOK(); |
| 651 | } |
| 652 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 653 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 654 | BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 655 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 656 | Ops[0].init(val, this); |
| 657 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 658 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 659 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | //===----------------------------------------------------------------------===// |
| 663 | // GetElementPtrInst Implementation |
| 664 | //===----------------------------------------------------------------------===// |
| 665 | |
| 666 | // checkType - Simple wrapper function to give a better assertion failure |
| 667 | // message on bad indexes for a gep instruction. |
| 668 | // |
| 669 | static inline const Type *checkType(const Type *Ty) { |
Chris Lattner | 47a6e63 | 2006-05-14 18:34:36 +0000 | [diff] [blame^] | 670 | assert(Ty && "Invalid GetElementPtrInst indices for type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 671 | return Ty; |
| 672 | } |
| 673 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 674 | void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) { |
| 675 | NumOperands = 1+Idx.size(); |
| 676 | Use *OL = OperandList = new Use[NumOperands]; |
| 677 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 678 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 679 | for (unsigned i = 0, e = Idx.size(); i != e; ++i) |
| 680 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 684 | NumOperands = 3; |
| 685 | Use *OL = OperandList = new Use[3]; |
| 686 | OL[0].init(Ptr, this); |
| 687 | OL[1].init(Idx0, this); |
| 688 | OL[2].init(Idx1, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 691 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 692 | NumOperands = 2; |
| 693 | Use *OL = OperandList = new Use[2]; |
| 694 | OL[0].init(Ptr, this); |
| 695 | OL[1].init(Idx, this); |
| 696 | } |
| 697 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 698 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 699 | const std::string &Name, Instruction *InBe) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 700 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 701 | Idx, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 702 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 703 | init(Ptr, Idx); |
| 704 | } |
| 705 | |
| 706 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 707 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 708 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 709 | Idx, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 710 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 711 | init(Ptr, Idx); |
| 712 | } |
| 713 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 714 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 715 | const std::string &Name, Instruction *InBe) |
| 716 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 717 | GetElementPtr, 0, 0, Name, InBe) { |
| 718 | init(Ptr, Idx); |
| 719 | } |
| 720 | |
| 721 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 722 | const std::string &Name, BasicBlock *IAE) |
| 723 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 724 | GetElementPtr, 0, 0, Name, IAE) { |
| 725 | init(Ptr, Idx); |
| 726 | } |
| 727 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 728 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 729 | const std::string &Name, Instruction *InBe) |
| 730 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 731 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 732 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 733 | init(Ptr, Idx0, Idx1); |
| 734 | } |
| 735 | |
| 736 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 737 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 738 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 739 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 740 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 741 | init(Ptr, Idx0, Idx1); |
| 742 | } |
| 743 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 744 | GetElementPtrInst::~GetElementPtrInst() { |
| 745 | delete[] OperandList; |
| 746 | } |
| 747 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 748 | // getIndexedType - Returns the type of the element that would be loaded with |
| 749 | // a load instruction with the specified parameters. |
| 750 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 751 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 752 | // pointer type. |
| 753 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 754 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 755 | const std::vector<Value*> &Idx, |
| 756 | bool AllowCompositeLeaf) { |
| 757 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 758 | |
| 759 | // Handle the special case of the empty set index set... |
| 760 | if (Idx.empty()) |
| 761 | if (AllowCompositeLeaf || |
| 762 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 763 | return cast<PointerType>(Ptr)->getElementType(); |
| 764 | else |
| 765 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 766 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 767 | unsigned CurIdx = 0; |
| 768 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| 769 | if (Idx.size() == CurIdx) { |
| 770 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 771 | return 0; // Can't load a whole structure or array!?!? |
| 772 | } |
| 773 | |
| 774 | Value *Index = Idx[CurIdx++]; |
| 775 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 776 | return 0; // Can only index into pointer types at the first index! |
| 777 | if (!CT->indexValid(Index)) return 0; |
| 778 | Ptr = CT->getTypeAtIndex(Index); |
| 779 | |
| 780 | // If the new type forwards to another type, then it is in the middle |
| 781 | // of being refined to another type (and hence, may have dropped all |
| 782 | // references to what it was using before). So, use the new forwarded |
| 783 | // type. |
| 784 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 785 | Ptr = Ty; |
| 786 | } |
| 787 | } |
| 788 | return CurIdx == Idx.size() ? Ptr : 0; |
| 789 | } |
| 790 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 791 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 792 | Value *Idx0, Value *Idx1, |
| 793 | bool AllowCompositeLeaf) { |
| 794 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 795 | if (!PTy) return 0; // Type isn't a pointer type! |
| 796 | |
| 797 | // Check the pointer index. |
| 798 | if (!PTy->indexValid(Idx0)) return 0; |
| 799 | |
| 800 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 801 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 802 | |
| 803 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 804 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 805 | return ElTy; |
| 806 | return 0; |
| 807 | } |
| 808 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 809 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 810 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 811 | if (!PTy) return 0; // Type isn't a pointer type! |
| 812 | |
| 813 | // Check the pointer index. |
| 814 | if (!PTy->indexValid(Idx)) return 0; |
| 815 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 816 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 819 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 820 | // ExtractElementInst Implementation |
| 821 | //===----------------------------------------------------------------------===// |
| 822 | |
| 823 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 824 | const std::string &Name, |
| 825 | Instruction *InsertBef) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 826 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 827 | ExtractElement, Ops, 2, Name, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 828 | assert(isValidOperands(Val, Index) && |
| 829 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 830 | Ops[0].init(Val, this); |
| 831 | Ops[1].init(Index, this); |
| 832 | } |
| 833 | |
| 834 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 835 | const std::string &Name, |
| 836 | BasicBlock *InsertAE) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 837 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 838 | ExtractElement, Ops, 2, Name, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 839 | assert(isValidOperands(Val, Index) && |
| 840 | "Invalid extractelement instruction operands!"); |
| 841 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 842 | Ops[0].init(Val, this); |
| 843 | Ops[1].init(Index, this); |
| 844 | } |
| 845 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 846 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
| 847 | if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy) |
| 848 | return false; |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 853 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 854 | // InsertElementInst Implementation |
| 855 | //===----------------------------------------------------------------------===// |
| 856 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 857 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 858 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 859 | Ops[0].init(IE.Ops[0], this); |
| 860 | Ops[1].init(IE.Ops[1], this); |
| 861 | Ops[2].init(IE.Ops[2], this); |
| 862 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 863 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 864 | const std::string &Name, |
| 865 | Instruction *InsertBef) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 866 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
| 867 | assert(isValidOperands(Vec, Elt, Index) && |
| 868 | "Invalid insertelement instruction operands!"); |
| 869 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 870 | Ops[1].init(Elt, this); |
| 871 | Ops[2].init(Index, this); |
| 872 | } |
| 873 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 874 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 875 | const std::string &Name, |
| 876 | BasicBlock *InsertAE) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 877 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
| 878 | assert(isValidOperands(Vec, Elt, Index) && |
| 879 | "Invalid insertelement instruction operands!"); |
| 880 | |
| 881 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 882 | Ops[1].init(Elt, this); |
| 883 | Ops[2].init(Index, this); |
| 884 | } |
| 885 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 886 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 887 | const Value *Index) { |
| 888 | if (!isa<PackedType>(Vec->getType())) |
| 889 | return false; // First operand of insertelement must be packed type. |
| 890 | |
| 891 | if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType()) |
| 892 | return false;// Second operand of insertelement must be packed element type. |
| 893 | |
| 894 | if (Index->getType() != Type::UIntTy) |
| 895 | return false; // Third operand of insertelement must be uint. |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 900 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 901 | // ShuffleVectorInst Implementation |
| 902 | //===----------------------------------------------------------------------===// |
| 903 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 904 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 905 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 906 | Ops[0].init(SV.Ops[0], this); |
| 907 | Ops[1].init(SV.Ops[1], this); |
| 908 | Ops[2].init(SV.Ops[2], this); |
| 909 | } |
| 910 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 911 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 912 | const std::string &Name, |
| 913 | Instruction *InsertBefore) |
| 914 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { |
| 915 | assert(isValidOperands(V1, V2, Mask) && |
| 916 | "Invalid shuffle vector instruction operands!"); |
| 917 | Ops[0].init(V1, this); |
| 918 | Ops[1].init(V2, this); |
| 919 | Ops[2].init(Mask, this); |
| 920 | } |
| 921 | |
| 922 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 923 | const std::string &Name, |
| 924 | BasicBlock *InsertAtEnd) |
| 925 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { |
| 926 | assert(isValidOperands(V1, V2, Mask) && |
| 927 | "Invalid shuffle vector instruction operands!"); |
| 928 | |
| 929 | Ops[0].init(V1, this); |
| 930 | Ops[1].init(V2, this); |
| 931 | Ops[2].init(Mask, this); |
| 932 | } |
| 933 | |
| 934 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 935 | const Value *Mask) { |
| 936 | if (!isa<PackedType>(V1->getType())) return false; |
| 937 | if (V1->getType() != V2->getType()) return false; |
| 938 | if (!isa<PackedType>(Mask->getType()) || |
| 939 | cast<PackedType>(Mask->getType())->getElementType() != Type::UIntTy || |
| 940 | cast<PackedType>(Mask->getType())->getNumElements() != |
| 941 | cast<PackedType>(V1->getType())->getNumElements()) |
| 942 | return false; |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | |
| 947 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 948 | // BinaryOperator Class |
| 949 | //===----------------------------------------------------------------------===// |
| 950 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 951 | void BinaryOperator::init(BinaryOps iType) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 952 | { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 953 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
| 954 | assert(LHS->getType() == RHS->getType() && |
| 955 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 956 | #ifndef NDEBUG |
| 957 | switch (iType) { |
| 958 | case Add: case Sub: |
| 959 | case Mul: case Div: |
| 960 | case Rem: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 961 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 962 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 963 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
| 964 | isa<PackedType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 965 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 966 | break; |
| 967 | case And: case Or: |
| 968 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 969 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 970 | "Logical operation should return same type as operands!"); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 971 | assert((getType()->isIntegral() || |
| 972 | (isa<PackedType>(getType()) && |
| 973 | cast<PackedType>(getType())->getElementType()->isIntegral())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 974 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 975 | break; |
| 976 | case SetLT: case SetGT: case SetLE: |
| 977 | case SetGE: case SetEQ: case SetNE: |
| 978 | assert(getType() == Type::BoolTy && "Setcc must return bool!"); |
| 979 | default: |
| 980 | break; |
| 981 | } |
| 982 | #endif |
| 983 | } |
| 984 | |
| 985 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 986 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 987 | Instruction *InsertBefore) { |
| 988 | assert(S1->getType() == S2->getType() && |
| 989 | "Cannot create binary operator with two operands of differing type!"); |
| 990 | switch (Op) { |
| 991 | // Binary comparison operators... |
| 992 | case SetLT: case SetGT: case SetLE: |
| 993 | case SetGE: case SetEQ: case SetNE: |
| 994 | return new SetCondInst(Op, S1, S2, Name, InsertBefore); |
| 995 | |
| 996 | default: |
| 997 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1002 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1003 | BasicBlock *InsertAtEnd) { |
| 1004 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1005 | InsertAtEnd->getInstList().push_back(Res); |
| 1006 | return Res; |
| 1007 | } |
| 1008 | |
| 1009 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1010 | Instruction *InsertBefore) { |
| 1011 | if (!Op->getType()->isFloatingPoint()) |
| 1012 | return new BinaryOperator(Instruction::Sub, |
| 1013 | Constant::getNullValue(Op->getType()), Op, |
| 1014 | Op->getType(), Name, InsertBefore); |
| 1015 | else |
| 1016 | return new BinaryOperator(Instruction::Sub, |
| 1017 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1018 | Op->getType(), Name, InsertBefore); |
| 1019 | } |
| 1020 | |
| 1021 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1022 | BasicBlock *InsertAtEnd) { |
| 1023 | if (!Op->getType()->isFloatingPoint()) |
| 1024 | return new BinaryOperator(Instruction::Sub, |
| 1025 | Constant::getNullValue(Op->getType()), Op, |
| 1026 | Op->getType(), Name, InsertAtEnd); |
| 1027 | else |
| 1028 | return new BinaryOperator(Instruction::Sub, |
| 1029 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1030 | Op->getType(), Name, InsertAtEnd); |
| 1031 | } |
| 1032 | |
| 1033 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1034 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1035 | Constant *C; |
| 1036 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1037 | C = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1038 | C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
| 1039 | } else { |
| 1040 | C = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1041 | } |
| 1042 | |
| 1043 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1044 | Op->getType(), Name, InsertBefore); |
| 1045 | } |
| 1046 | |
| 1047 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1048 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1049 | Constant *AllOnes; |
| 1050 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1051 | // Create a vector of all ones values. |
| 1052 | Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1053 | AllOnes = |
| 1054 | ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
| 1055 | } else { |
| 1056 | AllOnes = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1057 | } |
| 1058 | |
| 1059 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1060 | Op->getType(), Name, InsertAtEnd); |
| 1061 | } |
| 1062 | |
| 1063 | |
| 1064 | // isConstantAllOnes - Helper function for several functions below |
| 1065 | static inline bool isConstantAllOnes(const Value *V) { |
| 1066 | return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue(); |
| 1067 | } |
| 1068 | |
| 1069 | bool BinaryOperator::isNeg(const Value *V) { |
| 1070 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1071 | if (Bop->getOpcode() == Instruction::Sub) |
| 1072 | if (!V->getType()->isFloatingPoint()) |
| 1073 | return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); |
| 1074 | else |
| 1075 | return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | bool BinaryOperator::isNot(const Value *V) { |
| 1080 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1081 | return (Bop->getOpcode() == Instruction::Xor && |
| 1082 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1083 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1087 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1088 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1089 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1092 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1093 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1096 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1097 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1098 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1099 | Value *Op0 = BO->getOperand(0); |
| 1100 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1101 | if (isConstantAllOnes(Op0)) return Op1; |
| 1102 | |
| 1103 | assert(isConstantAllOnes(Op1)); |
| 1104 | return Op0; |
| 1105 | } |
| 1106 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1107 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1108 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | // swapOperands - Exchange the two operands to this instruction. This |
| 1113 | // instruction is safe to use on any binary instruction and does not |
| 1114 | // modify the semantics of the instruction. If the instruction is |
| 1115 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1116 | // |
| 1117 | bool BinaryOperator::swapOperands() { |
| 1118 | if (isCommutative()) |
| 1119 | ; // If the instruction is commutative, it is safe to swap the operands |
| 1120 | else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) |
| 1121 | /// FIXME: SetCC instructions shouldn't all have different opcodes. |
| 1122 | setOpcode(SCI->getSwappedCondition()); |
| 1123 | else |
| 1124 | return true; // Can't commute operands |
| 1125 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1126 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
| 1129 | |
| 1130 | |
| 1131 | //===----------------------------------------------------------------------===// |
| 1132 | // SetCondInst Class |
| 1133 | //===----------------------------------------------------------------------===// |
| 1134 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1135 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1136 | const std::string &Name, Instruction *InsertBefore) |
| 1137 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) { |
| 1138 | |
| 1139 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1140 | assert(getInverseCondition(Opcode)); |
| 1141 | } |
| 1142 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1143 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1144 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 1145 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) { |
| 1146 | |
| 1147 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1148 | assert(getInverseCondition(Opcode)); |
| 1149 | } |
| 1150 | |
| 1151 | // getInverseCondition - Return the inverse of the current condition opcode. |
| 1152 | // For example seteq -> setne, setgt -> setle, setlt -> setge, etc... |
| 1153 | // |
| 1154 | Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) { |
| 1155 | switch (Opcode) { |
| 1156 | default: |
| 1157 | assert(0 && "Unknown setcc opcode!"); |
| 1158 | case SetEQ: return SetNE; |
| 1159 | case SetNE: return SetEQ; |
| 1160 | case SetGT: return SetLE; |
| 1161 | case SetLT: return SetGE; |
| 1162 | case SetGE: return SetLT; |
| 1163 | case SetLE: return SetGT; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | // getSwappedCondition - Return the condition opcode that would be the result |
| 1168 | // of exchanging the two operands of the setcc instruction without changing |
| 1169 | // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. |
| 1170 | // |
| 1171 | Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) { |
| 1172 | switch (Opcode) { |
| 1173 | default: assert(0 && "Unknown setcc instruction!"); |
| 1174 | case SetEQ: case SetNE: return Opcode; |
| 1175 | case SetGT: return SetLT; |
| 1176 | case SetLT: return SetGT; |
| 1177 | case SetGE: return SetLE; |
| 1178 | case SetLE: return SetGE; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | //===----------------------------------------------------------------------===// |
| 1183 | // SwitchInst Implementation |
| 1184 | //===----------------------------------------------------------------------===// |
| 1185 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1186 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1187 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1188 | ReservedSpace = 2+NumCases*2; |
| 1189 | NumOperands = 2; |
| 1190 | OperandList = new Use[ReservedSpace]; |
| 1191 | |
| 1192 | OperandList[0].init(Value, this); |
| 1193 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1196 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1197 | : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], |
| 1198 | SI.getNumOperands()) { |
| 1199 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 1200 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 1201 | OL[i].init(InOL[i], this); |
| 1202 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1206 | SwitchInst::~SwitchInst() { |
| 1207 | delete [] OperandList; |
| 1208 | } |
| 1209 | |
| 1210 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1211 | /// addCase - Add an entry to the switch instruction... |
| 1212 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 1213 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1214 | unsigned OpNo = NumOperands; |
| 1215 | if (OpNo+2 > ReservedSpace) |
| 1216 | resizeOperands(0); // Get more space! |
| 1217 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1218 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1219 | NumOperands = OpNo+2; |
| 1220 | OperandList[OpNo].init(OnVal, this); |
| 1221 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /// removeCase - This method removes the specified successor from the switch |
| 1225 | /// instruction. Note that this cannot be used to remove the default |
| 1226 | /// destination (successor #0). |
| 1227 | /// |
| 1228 | void SwitchInst::removeCase(unsigned idx) { |
| 1229 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1230 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 1231 | |
| 1232 | unsigned NumOps = getNumOperands(); |
| 1233 | Use *OL = OperandList; |
| 1234 | |
| 1235 | // Move everything after this operand down. |
| 1236 | // |
| 1237 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 1238 | // client might not expect this to happen. The code as it is thrashes the |
| 1239 | // use/def lists, which is kinda lame. |
| 1240 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 1241 | OL[i-2] = OL[i]; |
| 1242 | OL[i-2+1] = OL[i+1]; |
| 1243 | } |
| 1244 | |
| 1245 | // Nuke the last value. |
| 1246 | OL[NumOps-2].set(0); |
| 1247 | OL[NumOps-2+1].set(0); |
| 1248 | NumOperands = NumOps-2; |
| 1249 | } |
| 1250 | |
| 1251 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 1252 | /// list according to the following behavior: |
| 1253 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 1254 | /// of operation. This grows the number of ops by 1.5 times. |
| 1255 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 1256 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 1257 | /// |
| 1258 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 1259 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1260 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1261 | } else if (NumOps*2 > NumOperands) { |
| 1262 | // No resize needed. |
| 1263 | if (ReservedSpace >= NumOps) return; |
| 1264 | } else if (NumOps == NumOperands) { |
| 1265 | if (ReservedSpace == NumOps) return; |
| 1266 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1267 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | ReservedSpace = NumOps; |
| 1271 | Use *NewOps = new Use[NumOps]; |
| 1272 | Use *OldOps = OperandList; |
| 1273 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1274 | NewOps[i].init(OldOps[i], this); |
| 1275 | OldOps[i].set(0); |
| 1276 | } |
| 1277 | delete [] OldOps; |
| 1278 | OperandList = NewOps; |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 1283 | return getSuccessor(idx); |
| 1284 | } |
| 1285 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 1286 | return getNumSuccessors(); |
| 1287 | } |
| 1288 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1289 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1290 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1291 | |
| 1292 | |
| 1293 | // Define these methods here so vtables don't get emitted into every translation |
| 1294 | // unit that uses these classes. |
| 1295 | |
| 1296 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 1297 | return new GetElementPtrInst(*this); |
| 1298 | } |
| 1299 | |
| 1300 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1301 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 1305 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1306 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1307 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 1308 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 1309 | CastInst *CastInst::clone() const { return new CastInst(*this); } |
| 1310 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
| 1311 | ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } |
| 1312 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1313 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1314 | ExtractElementInst *ExtractElementInst::clone() const { |
| 1315 | return new ExtractElementInst(*this); |
| 1316 | } |
| 1317 | InsertElementInst *InsertElementInst::clone() const { |
| 1318 | return new InsertElementInst(*this); |
| 1319 | } |
| 1320 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 1321 | return new ShuffleVectorInst(*this); |
| 1322 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1323 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 1324 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 1325 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 1326 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 1327 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 1328 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1329 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |