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