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