Intel SPMD Program Compiler  1.3.0
llvmutil.cpp
Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2010-2012, Intel Corporation
00003   All rights reserved.
00004 
00005   Redistribution and use in source and binary forms, with or without
00006   modification, are permitted provided that the following conditions are
00007   met:
00008 
00009     * Redistributions of source code must retain the above copyright
00010       notice, this list of conditions and the following disclaimer.
00011 
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015 
00016     * Neither the name of Intel Corporation nor the names of its
00017       contributors may be used to endorse or promote products derived from
00018       this software without specific prior written permission.
00019 
00020 
00021    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00022    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00023    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00024    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00025    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00032 */
00033 
00034 /** @file llvmutil.cpp
00035     @brief Implementations of various LLVM utility types and classes.
00036 */
00037 
00038 #include "llvmutil.h"
00039 #include "ispc.h"
00040 #include "type.h"
00041 #include <llvm/Instructions.h>
00042 #include <llvm/BasicBlock.h>
00043 #include <set>
00044 #include <map>
00045 
00046 llvm::Type *LLVMTypes::VoidType = NULL;
00047 llvm::PointerType *LLVMTypes::VoidPointerType = NULL;
00048 llvm::Type *LLVMTypes::PointerIntType = NULL;
00049 llvm::Type *LLVMTypes::BoolType = NULL;
00050 
00051 llvm::Type *LLVMTypes::Int8Type = NULL;
00052 llvm::Type *LLVMTypes::Int16Type = NULL;
00053 llvm::Type *LLVMTypes::Int32Type = NULL;
00054 llvm::Type *LLVMTypes::Int64Type = NULL;
00055 llvm::Type *LLVMTypes::FloatType = NULL;
00056 llvm::Type *LLVMTypes::DoubleType = NULL;
00057 
00058 llvm::Type *LLVMTypes::Int8PointerType = NULL;
00059 llvm::Type *LLVMTypes::Int16PointerType = NULL;
00060 llvm::Type *LLVMTypes::Int32PointerType = NULL;
00061 llvm::Type *LLVMTypes::Int64PointerType = NULL;
00062 llvm::Type *LLVMTypes::FloatPointerType = NULL;
00063 llvm::Type *LLVMTypes::DoublePointerType = NULL;
00064 
00065 llvm::VectorType *LLVMTypes::MaskType = NULL;
00066 llvm::VectorType *LLVMTypes::BoolVectorType = NULL;
00067 
00068 llvm::VectorType *LLVMTypes::Int1VectorType = NULL;
00069 llvm::VectorType *LLVMTypes::Int8VectorType = NULL;
00070 llvm::VectorType *LLVMTypes::Int16VectorType = NULL;
00071 llvm::VectorType *LLVMTypes::Int32VectorType = NULL;
00072 llvm::VectorType *LLVMTypes::Int64VectorType = NULL;
00073 llvm::VectorType *LLVMTypes::FloatVectorType = NULL;
00074 llvm::VectorType *LLVMTypes::DoubleVectorType = NULL;
00075 
00076 llvm::Type *LLVMTypes::Int8VectorPointerType = NULL;
00077 llvm::Type *LLVMTypes::Int16VectorPointerType = NULL;
00078 llvm::Type *LLVMTypes::Int32VectorPointerType = NULL;
00079 llvm::Type *LLVMTypes::Int64VectorPointerType = NULL;
00080 llvm::Type *LLVMTypes::FloatVectorPointerType = NULL;
00081 llvm::Type *LLVMTypes::DoubleVectorPointerType = NULL;
00082 
00083 llvm::VectorType *LLVMTypes::VoidPointerVectorType = NULL;
00084 
00085 llvm::Constant *LLVMTrue = NULL;
00086 llvm::Constant *LLVMFalse = NULL;
00087 llvm::Constant *LLVMMaskAllOn = NULL;
00088 llvm::Constant *LLVMMaskAllOff = NULL;
00089 
00090 
00091 void
00092 InitLLVMUtil(llvm::LLVMContext *ctx, Target target) {
00093     LLVMTypes::VoidType = llvm::Type::getVoidTy(*ctx);
00094     LLVMTypes::VoidPointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*ctx), 0);
00095     LLVMTypes::PointerIntType = target.is32Bit ? llvm::Type::getInt32Ty(*ctx) :
00096         llvm::Type::getInt64Ty(*ctx);
00097 
00098     LLVMTypes::BoolType = llvm::Type::getInt1Ty(*ctx);
00099     LLVMTypes::Int8Type = llvm::Type::getInt8Ty(*ctx);
00100     LLVMTypes::Int16Type = llvm::Type::getInt16Ty(*ctx);
00101     LLVMTypes::Int32Type = llvm::Type::getInt32Ty(*ctx);
00102     LLVMTypes::Int64Type = llvm::Type::getInt64Ty(*ctx);
00103     LLVMTypes::FloatType = llvm::Type::getFloatTy(*ctx);
00104     LLVMTypes::DoubleType = llvm::Type::getDoubleTy(*ctx);
00105 
00106     LLVMTypes::Int8PointerType = llvm::PointerType::get(LLVMTypes::Int8Type, 0);
00107     LLVMTypes::Int16PointerType = llvm::PointerType::get(LLVMTypes::Int16Type, 0);
00108     LLVMTypes::Int32PointerType = llvm::PointerType::get(LLVMTypes::Int32Type, 0);
00109     LLVMTypes::Int64PointerType = llvm::PointerType::get(LLVMTypes::Int64Type, 0);
00110     LLVMTypes::FloatPointerType = llvm::PointerType::get(LLVMTypes::FloatType, 0);
00111     LLVMTypes::DoublePointerType = llvm::PointerType::get(LLVMTypes::DoubleType, 0);
00112 
00113     if (target.maskBitCount == 1)
00114         LLVMTypes::MaskType = LLVMTypes::BoolVectorType =
00115             llvm::VectorType::get(llvm::Type::getInt1Ty(*ctx), target.vectorWidth);
00116     else {
00117         Assert(target.maskBitCount == 32);
00118         LLVMTypes::MaskType = LLVMTypes::BoolVectorType =
00119             llvm::VectorType::get(llvm::Type::getInt32Ty(*ctx), target.vectorWidth);
00120     }
00121 
00122     LLVMTypes::Int1VectorType = 
00123         llvm::VectorType::get(llvm::Type::getInt1Ty(*ctx), target.vectorWidth);
00124     LLVMTypes::Int8VectorType = 
00125         llvm::VectorType::get(LLVMTypes::Int8Type, target.vectorWidth);
00126     LLVMTypes::Int16VectorType = 
00127         llvm::VectorType::get(LLVMTypes::Int16Type, target.vectorWidth);
00128     LLVMTypes::Int32VectorType = 
00129         llvm::VectorType::get(LLVMTypes::Int32Type, target.vectorWidth);
00130     LLVMTypes::Int64VectorType = 
00131         llvm::VectorType::get(LLVMTypes::Int64Type, target.vectorWidth);
00132     LLVMTypes::FloatVectorType = 
00133         llvm::VectorType::get(LLVMTypes::FloatType, target.vectorWidth);
00134     LLVMTypes::DoubleVectorType = 
00135         llvm::VectorType::get(LLVMTypes::DoubleType, target.vectorWidth);
00136 
00137     LLVMTypes::Int8VectorPointerType = llvm::PointerType::get(LLVMTypes::Int8VectorType, 0);
00138     LLVMTypes::Int16VectorPointerType = llvm::PointerType::get(LLVMTypes::Int16VectorType, 0);
00139     LLVMTypes::Int32VectorPointerType = llvm::PointerType::get(LLVMTypes::Int32VectorType, 0);
00140     LLVMTypes::Int64VectorPointerType = llvm::PointerType::get(LLVMTypes::Int64VectorType, 0);
00141     LLVMTypes::FloatVectorPointerType = llvm::PointerType::get(LLVMTypes::FloatVectorType, 0);
00142     LLVMTypes::DoubleVectorPointerType = llvm::PointerType::get(LLVMTypes::DoubleVectorType, 0);
00143 
00144     LLVMTypes::VoidPointerVectorType = g->target.is32Bit ? LLVMTypes::Int32VectorType :
00145         LLVMTypes::Int64VectorType;
00146 
00147     LLVMTrue = llvm::ConstantInt::getTrue(*ctx);
00148     LLVMFalse = llvm::ConstantInt::getFalse(*ctx);
00149 
00150     std::vector<llvm::Constant *> maskOnes;
00151     llvm::Constant *onMask = NULL;
00152     if (target.maskBitCount == 1)
00153         onMask = llvm::ConstantInt::get(llvm::Type::getInt1Ty(*ctx), 1,
00154                                         false /*unsigned*/); // 0x1
00155     else
00156         onMask = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), -1,
00157                                     true /*signed*/); // 0xffffffff
00158 
00159     for (int i = 0; i < target.vectorWidth; ++i)
00160         maskOnes.push_back(onMask);
00161     LLVMMaskAllOn = llvm::ConstantVector::get(maskOnes);
00162 
00163     std::vector<llvm::Constant *> maskZeros;
00164     llvm::Constant *offMask = NULL;
00165     if (target.maskBitCount == 1)
00166         offMask = llvm::ConstantInt::get(llvm::Type::getInt1Ty(*ctx), 0,
00167                                          true /*signed*/);
00168     else
00169         offMask = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0,
00170                                          true /*signed*/);
00171 
00172     for (int i = 0; i < target.vectorWidth; ++i)
00173         maskZeros.push_back(offMask);
00174     LLVMMaskAllOff = llvm::ConstantVector::get(maskZeros);
00175 }
00176 
00177 
00178 llvm::ConstantInt *
00179 LLVMInt8(int8_t ival) {
00180     return llvm::ConstantInt::get(llvm::Type::getInt8Ty(*g->ctx), ival,
00181                                   true /*signed*/);
00182 }
00183 
00184 
00185 llvm::ConstantInt *
00186 LLVMUInt8(uint8_t ival) {
00187     return llvm::ConstantInt::get(llvm::Type::getInt8Ty(*g->ctx), ival,
00188                                   false /*unsigned*/);
00189 }
00190 
00191 
00192 llvm::ConstantInt *
00193 LLVMInt16(int16_t ival) {
00194     return llvm::ConstantInt::get(llvm::Type::getInt16Ty(*g->ctx), ival,
00195                                   true /*signed*/);
00196 }
00197 
00198 
00199 llvm::ConstantInt *
00200 LLVMUInt16(uint16_t ival) {
00201     return llvm::ConstantInt::get(llvm::Type::getInt16Ty(*g->ctx), ival,
00202                                   false /*unsigned*/);
00203 }
00204 
00205 
00206 llvm::ConstantInt *
00207 LLVMInt32(int32_t ival) {
00208     return llvm::ConstantInt::get(llvm::Type::getInt32Ty(*g->ctx), ival,
00209                                   true /*signed*/);
00210 }
00211 
00212 
00213 llvm::ConstantInt *
00214 LLVMUInt32(uint32_t ival) {
00215     return llvm::ConstantInt::get(llvm::Type::getInt32Ty(*g->ctx), ival,
00216                                   false /*unsigned*/);
00217 }
00218 
00219 
00220 llvm::ConstantInt *
00221 LLVMInt64(int64_t ival) {
00222     return llvm::ConstantInt::get(llvm::Type::getInt64Ty(*g->ctx), ival,
00223                                   true /*signed*/);
00224 }
00225 
00226 
00227 llvm::ConstantInt *
00228 LLVMUInt64(uint64_t ival) {
00229     return llvm::ConstantInt::get(llvm::Type::getInt64Ty(*g->ctx), ival,
00230                                   false /*unsigned*/);
00231 }
00232 
00233 
00234 llvm::Constant *
00235 LLVMFloat(float fval) {
00236     return llvm::ConstantFP::get(llvm::Type::getFloatTy(*g->ctx), fval);
00237 }
00238 
00239 
00240 llvm::Constant *
00241 LLVMDouble(double dval) {
00242     return llvm::ConstantFP::get(llvm::Type::getDoubleTy(*g->ctx), dval);
00243 }
00244 
00245 
00246 llvm::Constant *
00247 LLVMInt8Vector(int8_t ival) {
00248     llvm::Constant *v = LLVMInt8(ival);
00249     std::vector<llvm::Constant *> vals;
00250     for (int i = 0; i < g->target.vectorWidth; ++i)
00251         vals.push_back(v);
00252     return llvm::ConstantVector::get(vals);
00253 }
00254 
00255 
00256 llvm::Constant *
00257 LLVMInt8Vector(const int8_t *ivec) {
00258     std::vector<llvm::Constant *> vals;
00259     for (int i = 0; i < g->target.vectorWidth; ++i)
00260         vals.push_back(LLVMInt8(ivec[i]));
00261     return llvm::ConstantVector::get(vals);
00262 }
00263 
00264 
00265 llvm::Constant *
00266 LLVMUInt8Vector(uint8_t ival) {
00267     llvm::Constant *v = LLVMUInt8(ival);
00268     std::vector<llvm::Constant *> vals;
00269     for (int i = 0; i < g->target.vectorWidth; ++i)
00270         vals.push_back(v);
00271     return llvm::ConstantVector::get(vals);
00272 }
00273 
00274 
00275 llvm::Constant *
00276 LLVMUInt8Vector(const uint8_t *ivec) {
00277     std::vector<llvm::Constant *> vals;
00278     for (int i = 0; i < g->target.vectorWidth; ++i)
00279         vals.push_back(LLVMUInt8(ivec[i]));
00280     return llvm::ConstantVector::get(vals);
00281 }
00282 
00283 
00284 llvm::Constant *
00285 LLVMInt16Vector(int16_t ival) {
00286     llvm::Constant *v = LLVMInt16(ival);
00287     std::vector<llvm::Constant *> vals;
00288     for (int i = 0; i < g->target.vectorWidth; ++i)
00289         vals.push_back(v);
00290     return llvm::ConstantVector::get(vals);
00291 }
00292 
00293 
00294 llvm::Constant *
00295 LLVMInt16Vector(const int16_t *ivec) {
00296     std::vector<llvm::Constant *> vals;
00297     for (int i = 0; i < g->target.vectorWidth; ++i)
00298         vals.push_back(LLVMInt16(ivec[i]));
00299     return llvm::ConstantVector::get(vals);
00300 }
00301 
00302 
00303 llvm::Constant *
00304 LLVMUInt16Vector(uint16_t ival) {
00305     llvm::Constant *v = LLVMUInt16(ival);
00306     std::vector<llvm::Constant *> vals;
00307     for (int i = 0; i < g->target.vectorWidth; ++i)
00308         vals.push_back(v);
00309     return llvm::ConstantVector::get(vals);
00310 }
00311 
00312 
00313 llvm::Constant *
00314 LLVMUInt16Vector(const uint16_t *ivec) {
00315     std::vector<llvm::Constant *> vals;
00316     for (int i = 0; i < g->target.vectorWidth; ++i)
00317         vals.push_back(LLVMUInt16(ivec[i]));
00318     return llvm::ConstantVector::get(vals);
00319 }
00320 
00321 
00322 llvm::Constant *
00323 LLVMInt32Vector(int32_t ival) {
00324     llvm::Constant *v = LLVMInt32(ival);
00325     std::vector<llvm::Constant *> vals;
00326     for (int i = 0; i < g->target.vectorWidth; ++i)
00327         vals.push_back(v);
00328     return llvm::ConstantVector::get(vals);
00329 }
00330 
00331 
00332 llvm::Constant *
00333 LLVMInt32Vector(const int32_t *ivec) {
00334     std::vector<llvm::Constant *> vals;
00335     for (int i = 0; i < g->target.vectorWidth; ++i)
00336         vals.push_back(LLVMInt32(ivec[i]));
00337     return llvm::ConstantVector::get(vals);
00338 }
00339 
00340 
00341 llvm::Constant *
00342 LLVMUInt32Vector(uint32_t ival) {
00343     llvm::Constant *v = LLVMUInt32(ival);
00344     std::vector<llvm::Constant *> vals;
00345     for (int i = 0; i < g->target.vectorWidth; ++i)
00346         vals.push_back(v);
00347     return llvm::ConstantVector::get(vals);
00348 }
00349 
00350 
00351 llvm::Constant *
00352 LLVMUInt32Vector(const uint32_t *ivec) {
00353     std::vector<llvm::Constant *> vals;
00354     for (int i = 0; i < g->target.vectorWidth; ++i)
00355         vals.push_back(LLVMUInt32(ivec[i]));
00356     return llvm::ConstantVector::get(vals);
00357 }
00358 
00359 
00360 llvm::Constant *
00361 LLVMFloatVector(float fval) {
00362     llvm::Constant *v = LLVMFloat(fval);
00363     std::vector<llvm::Constant *> vals;
00364     for (int i = 0; i < g->target.vectorWidth; ++i)
00365         vals.push_back(v);
00366     return llvm::ConstantVector::get(vals);
00367 }
00368 
00369 
00370 llvm::Constant *
00371 LLVMFloatVector(const float *fvec) {
00372     std::vector<llvm::Constant *> vals;
00373     for (int i = 0; i < g->target.vectorWidth; ++i)
00374         vals.push_back(LLVMFloat(fvec[i]));
00375     return llvm::ConstantVector::get(vals);
00376 }
00377 
00378 
00379 llvm::Constant *
00380 LLVMDoubleVector(double dval) {
00381     llvm::Constant *v = LLVMDouble(dval);
00382     std::vector<llvm::Constant *> vals;
00383     for (int i = 0; i < g->target.vectorWidth; ++i)
00384         vals.push_back(v);
00385     return llvm::ConstantVector::get(vals);
00386 }
00387 
00388 
00389 llvm::Constant *
00390 LLVMDoubleVector(const double *dvec) {
00391     std::vector<llvm::Constant *> vals;
00392     for (int i = 0; i < g->target.vectorWidth; ++i)
00393         vals.push_back(LLVMDouble(dvec[i]));
00394     return llvm::ConstantVector::get(vals);
00395 }
00396 
00397 
00398 llvm::Constant *
00399 LLVMInt64Vector(int64_t ival) {
00400     llvm::Constant *v = LLVMInt64(ival);
00401     std::vector<llvm::Constant *> vals;
00402     for (int i = 0; i < g->target.vectorWidth; ++i)
00403         vals.push_back(v);
00404     return llvm::ConstantVector::get(vals);
00405 }
00406 
00407 
00408 llvm::Constant *
00409 LLVMInt64Vector(const int64_t *ivec) {
00410     std::vector<llvm::Constant *> vals;
00411     for (int i = 0; i < g->target.vectorWidth; ++i)
00412         vals.push_back(LLVMInt64(ivec[i]));
00413     return llvm::ConstantVector::get(vals);
00414 }
00415 
00416 
00417 llvm::Constant *
00418 LLVMUInt64Vector(uint64_t ival) {
00419     llvm::Constant *v = LLVMUInt64(ival);
00420     std::vector<llvm::Constant *> vals;
00421     for (int i = 0; i < g->target.vectorWidth; ++i)
00422         vals.push_back(v);
00423     return llvm::ConstantVector::get(vals);
00424 }
00425 
00426 
00427 llvm::Constant *
00428 LLVMUInt64Vector(const uint64_t *ivec) {
00429     std::vector<llvm::Constant *> vals;
00430     for (int i = 0; i < g->target.vectorWidth; ++i)
00431         vals.push_back(LLVMUInt64(ivec[i]));
00432     return llvm::ConstantVector::get(vals);
00433 }
00434 
00435 
00436 llvm::Constant *
00437 LLVMBoolVector(bool b) {
00438     llvm::Constant *v;
00439     if (LLVMTypes::BoolVectorType == LLVMTypes::Int32VectorType) 
00440         v = llvm::ConstantInt::get(LLVMTypes::Int32Type, b ? 0xffffffff : 0, 
00441                                    false /*unsigned*/);
00442     else {
00443         Assert(LLVMTypes::BoolVectorType->getElementType() == 
00444                llvm::Type::getInt1Ty(*g->ctx));
00445         v = b ? LLVMTrue : LLVMFalse;
00446     }
00447 
00448     std::vector<llvm::Constant *> vals;
00449     for (int i = 0; i < g->target.vectorWidth; ++i)
00450         vals.push_back(v);
00451     return llvm::ConstantVector::get(vals);
00452 }
00453 
00454 
00455 llvm::Constant *
00456 LLVMBoolVector(const bool *bvec) {
00457     std::vector<llvm::Constant *> vals;
00458     for (int i = 0; i < g->target.vectorWidth; ++i) {
00459         llvm::Constant *v;
00460         if (LLVMTypes::BoolVectorType == LLVMTypes::Int32VectorType) 
00461             v = llvm::ConstantInt::get(LLVMTypes::Int32Type, bvec[i] ? 0xffffffff : 0, 
00462                                        false /*unsigned*/);
00463         else {
00464             Assert(LLVMTypes::BoolVectorType->getElementType() == 
00465                    llvm::Type::getInt1Ty(*g->ctx));
00466             v = bvec[i] ? LLVMTrue : LLVMFalse;
00467         }
00468 
00469         vals.push_back(v);
00470     }
00471     return llvm::ConstantVector::get(vals);
00472 }
00473 
00474 
00475 llvm::Constant *
00476 LLVMIntAsType(int64_t val, llvm::Type *type) {
00477     llvm::VectorType *vecType =
00478         llvm::dyn_cast<llvm::VectorType>(type);
00479 
00480     if (vecType != NULL) {
00481         llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(),
00482                                                    val, true /* signed */);
00483         std::vector<llvm::Constant *> vals;
00484         for (int i = 0; i < (int)vecType->getNumElements(); ++i)
00485             vals.push_back(v);
00486         return llvm::ConstantVector::get(vals);
00487     }
00488     else
00489         return llvm::ConstantInt::get(type, val, true /* signed */);
00490 }
00491 
00492 
00493 llvm::Constant *
00494 LLVMUIntAsType(uint64_t val, llvm::Type *type) {
00495     llvm::VectorType *vecType =
00496         llvm::dyn_cast<llvm::VectorType>(type);
00497 
00498     if (vecType != NULL) {
00499         llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(),
00500                                                    val, false /* unsigned */);
00501         std::vector<llvm::Constant *> vals;
00502         for (int i = 0; i < (int)vecType->getNumElements(); ++i)
00503             vals.push_back(v);
00504         return llvm::ConstantVector::get(vals);
00505     }
00506     else
00507         return llvm::ConstantInt::get(type, val, false /* unsigned */);
00508 }
00509 
00510 
00511 /** Conservative test to see if two llvm::Values are equal.  There are
00512     (potentially many) cases where the two values actually are equal but
00513     this will return false.  However, if it does return true, the two
00514     vectors definitely are equal.
00515 */
00516 static bool
00517 lValuesAreEqual(llvm::Value *v0, llvm::Value *v1, 
00518                 std::vector<llvm::PHINode *> &seenPhi0,
00519                 std::vector<llvm::PHINode *> &seenPhi1) {
00520     // Thanks to the fact that LLVM hashes and returns the same pointer for
00521     // constants (of all sorts, even constant expressions), this first test
00522     // actually catches a lot of cases.  LLVM's SSA form also helps a lot
00523     // with this..
00524     if (v0 == v1)
00525         return true;
00526 
00527     Assert(seenPhi0.size() == seenPhi1.size());
00528     for (unsigned int i = 0; i < seenPhi0.size(); ++i)
00529         if (v0 == seenPhi0[i] && v1 == seenPhi1[i])
00530             return true;
00531 
00532     llvm::BinaryOperator *bo0 = llvm::dyn_cast<llvm::BinaryOperator>(v0);
00533     llvm::BinaryOperator *bo1 = llvm::dyn_cast<llvm::BinaryOperator>(v1);
00534     if (bo0 != NULL && bo1 != NULL) {
00535         if (bo0->getOpcode() != bo1->getOpcode())
00536             return false;
00537         return (lValuesAreEqual(bo0->getOperand(0), bo1->getOperand(0),
00538                                 seenPhi0, seenPhi1) &&
00539                 lValuesAreEqual(bo0->getOperand(1), bo1->getOperand(1),
00540                                 seenPhi0, seenPhi1));
00541     }
00542 
00543     llvm::CastInst *cast0 = llvm::dyn_cast<llvm::CastInst>(v0);
00544     llvm::CastInst *cast1 = llvm::dyn_cast<llvm::CastInst>(v1);
00545     if (cast0 != NULL && cast1 != NULL) {
00546         if (cast0->getOpcode() != cast1->getOpcode())
00547             return NULL;
00548         return lValuesAreEqual(cast0->getOperand(0), cast1->getOperand(0),
00549                                seenPhi0, seenPhi1);
00550     }
00551 
00552     llvm::PHINode *phi0 = llvm::dyn_cast<llvm::PHINode>(v0);
00553     llvm::PHINode *phi1 = llvm::dyn_cast<llvm::PHINode>(v1);
00554     if (phi0 != NULL && phi1 != NULL) {
00555         if (phi0->getNumIncomingValues() != phi1->getNumIncomingValues())
00556             return false;
00557 
00558         seenPhi0.push_back(phi0);
00559         seenPhi1.push_back(phi1);
00560 
00561         unsigned int numIncoming = phi0->getNumIncomingValues();
00562         // Check all of the incoming values: if all of them are all equal,
00563         // then we're good.
00564         bool anyFailure = false;
00565         for (unsigned int i = 0; i < numIncoming; ++i) {
00566             // FIXME: should it be ok if the incoming blocks are different,
00567             // where we just return faliure in this case?
00568             Assert(phi0->getIncomingBlock(i) == phi1->getIncomingBlock(i));
00569             if (!lValuesAreEqual(phi0->getIncomingValue(i), 
00570                                  phi1->getIncomingValue(i), seenPhi0, seenPhi1)) {
00571                 anyFailure = true;
00572                 break;
00573             }
00574         }
00575 
00576         seenPhi0.pop_back();
00577         seenPhi1.pop_back();
00578 
00579         return !anyFailure;
00580     }
00581 
00582     return false;
00583 }
00584 
00585 
00586 /** Given an llvm::Value known to be an integer, return its value as
00587     an int64_t.
00588 */
00589 static int64_t
00590 lGetIntValue(llvm::Value *offset) {
00591     llvm::ConstantInt *intOffset = llvm::dyn_cast<llvm::ConstantInt>(offset);
00592     Assert(intOffset && (intOffset->getBitWidth() == 32 ||
00593                          intOffset->getBitWidth() == 64));
00594     return intOffset->getSExtValue();
00595 }
00596 
00597 
00598 void
00599 LLVMFlattenInsertChain(llvm::InsertElementInst *ie, int vectorWidth,
00600                        llvm::Value **elements) {
00601     for (int i = 0; i < vectorWidth; ++i)
00602         elements[i] = NULL;
00603 
00604     while (ie != NULL) {
00605         int64_t iOffset = lGetIntValue(ie->getOperand(2));
00606         Assert(iOffset >= 0 && iOffset < vectorWidth);
00607         Assert(elements[iOffset] == NULL);
00608 
00609         // Get the scalar value from this insert 
00610         elements[iOffset] = ie->getOperand(1);
00611 
00612         // Do we have another insert?
00613         llvm::Value *insertBase = ie->getOperand(0);
00614         ie = llvm::dyn_cast<llvm::InsertElementInst>(insertBase);
00615         if (ie == NULL) {
00616             if (llvm::isa<llvm::UndefValue>(insertBase))
00617                 return;
00618 
00619             // Get the value out of a constant vector if that's what we
00620             // have
00621             llvm::ConstantVector *cv = 
00622                 llvm::dyn_cast<llvm::ConstantVector>(insertBase);
00623 
00624             // FIXME: this assert is a little questionable; we probably
00625             // shouldn't fail in this case but should just return an
00626             // incomplete result.  But there aren't currently any known
00627             // cases where we have anything other than an undef value or a
00628             // constant vector at the base, so if that ever does happen,
00629             // it'd be nice to know what happend so that perhaps we can
00630             // handle it.
00631             // FIXME: Also, should we handle ConstantDataVectors with
00632             // LLVM3.1?  What about ConstantAggregateZero values??
00633             Assert(cv != NULL);
00634 
00635             Assert(iOffset < (int)cv->getNumOperands());
00636             elements[iOffset] = cv->getOperand((int32_t)iOffset);
00637         }
00638     }
00639 }
00640 
00641 
00642 bool
00643 LLVMExtractVectorInts(llvm::Value *v, int64_t ret[], int *nElts) {
00644     // Make sure we do in fact have a vector of integer values here
00645     llvm::VectorType *vt =
00646         llvm::dyn_cast<llvm::VectorType>(v->getType());
00647     Assert(vt != NULL);
00648     Assert(llvm::isa<llvm::IntegerType>(vt->getElementType()));
00649 
00650     *nElts = (int)vt->getNumElements();
00651 
00652     if (llvm::isa<llvm::ConstantAggregateZero>(v)) {
00653         for (int i = 0; i < (int)vt->getNumElements(); ++i)
00654             ret[i] = 0;
00655         return true;
00656     }
00657 
00658     // Deal with the fact that LLVM3.1 and previous versions have different
00659     // representations for vectors of constant ints...
00660 #ifndef LLVM_3_0
00661     llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
00662     if (cv == NULL)
00663         return false;
00664 
00665     for (int i = 0; i < (int)cv->getNumElements(); ++i)
00666         ret[i] = cv->getElementAsInteger(i);
00667     return true;
00668 #else
00669     llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v);
00670     if (cv == NULL)
00671         return false;
00672 
00673      llvm::SmallVector<llvm::Constant *, ISPC_MAX_NVEC> elements;
00674      cv->getVectorElements(elements);
00675      for (int i = 0; i < (int)vt->getNumElements(); ++i) {
00676          llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(elements[i]);
00677          Assert(ci != NULL);
00678          ret[i] = ci->getSExtValue();
00679      }
00680      return true;
00681 #endif // !LLVM_3_0
00682 }
00683 
00684 
00685 static bool
00686 lVectorValuesAllEqual(llvm::Value *v, int vectorLength,
00687                       std::vector<llvm::PHINode *> &seenPhis);
00688 
00689 
00690 /** This function checks to see if the given (scalar or vector) value is an
00691     exact multiple of baseValue.  It returns true if so, and false if not
00692     (or if it's not able to determine if it is).  Any vector value passed
00693     in is required to have the same value in all elements (so that we can
00694     just check the first element to be a multiple of the given value.)
00695  */
00696 static bool
00697 lIsExactMultiple(llvm::Value *val, int baseValue, int vectorLength,
00698                  std::vector<llvm::PHINode *> &seenPhis) {
00699     if (llvm::isa<llvm::VectorType>(val->getType()) == false) {
00700         // If we've worked down to a constant int, then the moment of truth
00701         // has arrived...
00702         llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(val);
00703         if (ci != NULL)
00704             return (ci->getZExtValue() % baseValue) == 0;
00705     }
00706     else
00707         Assert(LLVMVectorValuesAllEqual(val));
00708 
00709     llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(val);
00710     if (ie != NULL) {
00711         llvm::Value *elts[ISPC_MAX_NVEC];
00712         LLVMFlattenInsertChain(ie, g->target.vectorWidth, elts);
00713         // We just need to check the scalar first value, since we know that
00714         // all elements are equal
00715         return lIsExactMultiple(elts[0], baseValue, vectorLength,
00716                                      seenPhis);
00717     }
00718 
00719     llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(val);
00720     if (phi != NULL) {
00721         for (unsigned int i = 0; i < seenPhis.size(); ++i)
00722             if (phi == seenPhis[i])
00723                 return true;
00724 
00725         seenPhis.push_back(phi);
00726         unsigned int numIncoming = phi->getNumIncomingValues();
00727 
00728         // Check all of the incoming values: if all of them pass, then
00729         // we're good.
00730         for (unsigned int i = 0; i < numIncoming; ++i) {
00731             llvm::Value *incoming = phi->getIncomingValue(i);
00732             bool mult = lIsExactMultiple(incoming, baseValue, vectorLength, 
00733                                               seenPhis);
00734             if (mult == false) {
00735                 seenPhis.pop_back();
00736                 return false;
00737             }
00738         }
00739         seenPhis.pop_back();
00740         return true;
00741     }
00742 
00743     llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(val);
00744     if (bop != NULL && bop->getOpcode() == llvm::Instruction::Add) {
00745         llvm::Value *op0 = bop->getOperand(0);
00746         llvm::Value *op1 = bop->getOperand(1);
00747 
00748         bool be0 = lIsExactMultiple(op0, baseValue, vectorLength, seenPhis);
00749         bool be1 = lIsExactMultiple(op1, baseValue, vectorLength, seenPhis);
00750         return (be0 && be1);
00751     }
00752     // FIXME: mul? casts? ... ?
00753 
00754     return false;
00755 }
00756 
00757 
00758 /** Returns the next power of two greater than or equal to the given
00759     value. */
00760 static int
00761 lRoundUpPow2(int v) {
00762     v--;
00763     v |= v >> 1;    
00764     v |= v >> 2;
00765     v |= v >> 4;    
00766     v |= v >> 8;
00767     v |= v >> 16;
00768     return v+1;
00769 }
00770 
00771 
00772 /** Try to determine if all of the elements of the given vector value have
00773     the same value when divided by the given baseValue.  The function
00774     returns true if this can be determined to be the case, and false
00775     otherwise.  (This function may fail to identify some cases where it
00776     does in fact have this property, but should never report a given value
00777     as being a multiple if it isn't!)
00778  */
00779 static bool
00780 lAllDivBaseEqual(llvm::Value *val, int64_t baseValue, int vectorLength,
00781                  std::vector<llvm::PHINode *> &seenPhis,
00782                  bool &canAdd) {
00783     Assert(llvm::isa<llvm::VectorType>(val->getType()));
00784     // Make sure the base value is a positive power of 2
00785     Assert(baseValue > 0 && (baseValue & (baseValue-1)) == 0);
00786 
00787     // The easy case
00788     if (lVectorValuesAllEqual(val, vectorLength, seenPhis))
00789         return true;
00790 
00791     int64_t vecVals[ISPC_MAX_NVEC];
00792     int nElts;
00793     if (llvm::isa<llvm::VectorType>(val->getType()) &&
00794         LLVMExtractVectorInts(val, vecVals, &nElts)) {
00795         // If we have a vector of compile-time constant integer values,
00796         // then go ahead and check them directly..
00797         int64_t firstDiv = vecVals[0] / baseValue;
00798         for (int i = 1; i < nElts; ++i)
00799             if ((vecVals[i] / baseValue) != firstDiv)
00800                 return false;
00801 
00802         return true;
00803     }
00804 
00805     llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(val);
00806     if (phi != NULL) {
00807         for (unsigned int i = 0; i < seenPhis.size(); ++i)
00808             if (phi == seenPhis[i])
00809                 return true;
00810 
00811         seenPhis.push_back(phi);
00812         unsigned int numIncoming = phi->getNumIncomingValues();
00813 
00814         // Check all of the incoming values: if all of them pass, then
00815         // we're good.
00816         for (unsigned int i = 0; i < numIncoming; ++i) {
00817             llvm::Value *incoming = phi->getIncomingValue(i);
00818             bool ca = canAdd;
00819             bool mult = lAllDivBaseEqual(incoming, baseValue, vectorLength, 
00820                                          seenPhis, ca);
00821             if (mult == false) {
00822                 seenPhis.pop_back();
00823                 return false;
00824             }
00825         }
00826         seenPhis.pop_back();
00827         return true;
00828     }
00829 
00830     llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(val);
00831     if (bop != NULL && bop->getOpcode() == llvm::Instruction::Add && 
00832         canAdd == true) {
00833         llvm::Value *op0 = bop->getOperand(0);
00834         llvm::Value *op1 = bop->getOperand(1);
00835 
00836         // Otherwise we're only going to worry about the following case,
00837         // which comes up often when looping over SOA data:
00838         // ashr %val, <constant shift>
00839         // where %val = add %smear, <0,1,2,3...>
00840         // and where the maximum of the <0,...> vector in the add is less than
00841         //   1<<(constant shift),
00842         // and where %smear is a smear of a value that is a multiple of
00843         //   baseValue.
00844 
00845         int64_t addConstants[ISPC_MAX_NVEC];
00846         if (LLVMExtractVectorInts(op1, addConstants, &nElts) == false)
00847             return false;
00848         Assert(nElts == vectorLength);
00849 
00850         // Do all of them give the same value when divided by baseValue?
00851         int64_t firstConstDiv = addConstants[0] / baseValue;
00852         for (int i = 1; i < vectorLength; ++i)
00853             if ((addConstants[i] / baseValue) != firstConstDiv)
00854                 return false;
00855 
00856         if (lVectorValuesAllEqual(op0, vectorLength, seenPhis) == false)
00857             return false;
00858 
00859         // Note that canAdd is a reference parameter; setting this ensures
00860         // that we don't allow multiple adds in other parts of the chain of
00861         // dependent values from here.
00862         canAdd = false;
00863 
00864         // Now we need to figure out the required alignment (in numbers of
00865         // elements of the underlying type being indexed) of the value to
00866         // which these integer addConstant[] values are being added to.  We
00867         // know that we have addConstant[] values that all give the same
00868         // value when divided by baseValue, but we may need a less-strict
00869         // alignment than baseValue depending on the actual values.
00870         //
00871         // As an example, consider a case where the baseValue alignment is
00872         // 16, but the addConstants here are <0,1,2,3>.  In that case, the
00873         // value to which addConstants is added to only needs to be a
00874         // multiple of 4.  Conversely, if addConstants are <4,5,6,7>, then
00875         // we need a multiple of 8 to ensure that the final added result
00876         // will still have the same value for all vector elements when
00877         // divided by baseValue.
00878         //
00879         // All that said, here we need to find the maximum value of any of
00880         // the addConstants[], mod baseValue.  If we round that up to the
00881         // next power of 2, we'll have a value that will be no greater than
00882         // baseValue and sometimes less.
00883         int maxMod = int(addConstants[0] % baseValue);
00884         for (int i = 1; i < vectorLength; ++i)
00885             maxMod = std::max(maxMod, int(addConstants[i] % baseValue));
00886         int requiredAlignment = lRoundUpPow2(maxMod);
00887 
00888         std::vector<llvm::PHINode *> seenPhisEEM;
00889         return lIsExactMultiple(op0, requiredAlignment, vectorLength,
00890                                 seenPhisEEM);
00891     }
00892     // TODO: could handle mul by a vector of equal constant integer values
00893     // and the like here and adjust the 'baseValue' value when it evenly
00894     // divides, but unclear if it's worthwhile...
00895 
00896     return false;
00897 }
00898 
00899 
00900 /** Given a vector shift right of some value by some amount, try to
00901     determine if all of the elements of the final result have the same
00902     value (i.e. whether the high bits are all equal, disregarding the low
00903     bits that are shifted out.)  Returns true if so, and false otherwise.
00904  */
00905 static bool
00906 lVectorShiftRightAllEqual(llvm::Value *val, llvm::Value *shift,
00907                           int vectorLength) {
00908     // Are we shifting all elements by a compile-time constant amount?  If
00909     // not, give up.
00910     int64_t shiftAmount[ISPC_MAX_NVEC];
00911     int nElts;
00912     if (LLVMExtractVectorInts(shift, shiftAmount, &nElts) == false)
00913         return false;
00914     Assert(nElts == vectorLength);
00915 
00916     // Is it the same amount for all elements?
00917     for (int i = 0; i < vectorLength; ++i)
00918         if (shiftAmount[i] != shiftAmount[0])
00919             return false;
00920 
00921     // Now see if the value divided by (1 << shift) can be determined to
00922     // have the same value for all vector elements.
00923     int pow2 = 1 << shiftAmount[0];
00924     bool canAdd = true;
00925     std::vector<llvm::PHINode *> seenPhis;
00926     bool eq = lAllDivBaseEqual(val, pow2, vectorLength, seenPhis, canAdd);
00927 #if 0
00928     fprintf(stderr, "check all div base equal:\n");
00929     LLVMDumpValue(shift);
00930     LLVMDumpValue(val);
00931     fprintf(stderr, "----> %s\n\n", eq ? "true" : "false");
00932 #endif
00933     return eq;
00934 }
00935 
00936 
00937 static bool
00938 lVectorValuesAllEqual(llvm::Value *v, int vectorLength,
00939                       std::vector<llvm::PHINode *> &seenPhis) {
00940     if (vectorLength == 1)
00941         return true;
00942 
00943     if (llvm::isa<llvm::ConstantAggregateZero>(v))
00944         return true;
00945 
00946     llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v);
00947     if (cv != NULL)
00948         return (cv->getSplatValue() != NULL);
00949 
00950 #ifndef LLVM_3_0
00951     llvm::ConstantDataVector *cdv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
00952     if (cdv != NULL)
00953         return (cdv->getSplatValue() != NULL);
00954 #endif
00955 
00956     llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
00957     if (bop != NULL) {
00958         // Easy case: both operands are all equal -> return true
00959         if (lVectorValuesAllEqual(bop->getOperand(0), vectorLength, 
00960                                   seenPhis) &&
00961             lVectorValuesAllEqual(bop->getOperand(1), vectorLength, 
00962                                   seenPhis))
00963             return true;
00964 
00965         // If it's a shift, take a special path that tries to check if the
00966         // high (surviving) bits of the values are equal.
00967         if (bop->getOpcode() == llvm::Instruction::AShr ||
00968             bop->getOpcode() == llvm::Instruction::LShr)
00969             return lVectorShiftRightAllEqual(bop->getOperand(0), 
00970                                              bop->getOperand(1), vectorLength);
00971 
00972         return false;
00973     }
00974 
00975     llvm::CastInst *cast = llvm::dyn_cast<llvm::CastInst>(v);
00976     if (cast != NULL)
00977         return lVectorValuesAllEqual(cast->getOperand(0), vectorLength, 
00978                                      seenPhis);
00979 
00980     llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(v);
00981     if (ie != NULL) {
00982         llvm::Value *elements[ISPC_MAX_NVEC];
00983         LLVMFlattenInsertChain(ie, vectorLength, elements);
00984 
00985         // We will ignore any values of elements[] that are NULL; as they
00986         // correspond to undefined values--we just want to see if all of
00987         // the defined values have the same value.
00988         int lastNonNull = 0;
00989         while (lastNonNull < vectorLength && elements[lastNonNull] == NULL)
00990             ++lastNonNull;
00991 
00992         if (lastNonNull == vectorLength)
00993             // all of them are undef!
00994             return true;
00995 
00996         for (int i = lastNonNull; i < vectorLength; ++i) {
00997             if (elements[i] == NULL)
00998                 continue;
00999 
01000             std::vector<llvm::PHINode *> seenPhi0;
01001             std::vector<llvm::PHINode *> seenPhi1;
01002             if (lValuesAreEqual(elements[lastNonNull], elements[i], seenPhi0, 
01003                                 seenPhi1) == false)
01004                 return false;
01005             lastNonNull = i;
01006         }
01007         return true;
01008     }
01009 
01010     llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
01011     if (phi) {
01012         for (unsigned int i = 0; i < seenPhis.size(); ++i)
01013             if (seenPhis[i] == phi)
01014                 return true;
01015 
01016         seenPhis.push_back(phi);
01017 
01018         unsigned int numIncoming = phi->getNumIncomingValues();
01019         // Check all of the incoming values: if all of them are all equal,
01020         // then we're good.
01021         for (unsigned int i = 0; i < numIncoming; ++i) {
01022             if (!lVectorValuesAllEqual(phi->getIncomingValue(i), vectorLength,
01023                                        seenPhis)) {
01024                 seenPhis.pop_back();
01025                 return false;
01026             }
01027         }
01028 
01029         seenPhis.pop_back();
01030         return true;
01031     }
01032 
01033     if (llvm::isa<llvm::UndefValue>(v))
01034         // ?
01035         return false;
01036 
01037     Assert(!llvm::isa<llvm::Constant>(v));
01038 
01039     if (llvm::isa<llvm::CallInst>(v) || llvm::isa<llvm::LoadInst>(v) ||
01040         !llvm::isa<llvm::Instruction>(v))
01041         return false;
01042 
01043     llvm::ShuffleVectorInst *shuffle = llvm::dyn_cast<llvm::ShuffleVectorInst>(v);
01044     if (shuffle != NULL) {
01045         llvm::Value *indices = shuffle->getOperand(2);
01046         if (lVectorValuesAllEqual(indices, vectorLength, seenPhis))
01047             // The easy case--just a smear of the same element across the
01048             // whole vector.
01049             return true;
01050 
01051         // TODO: handle more general cases?
01052         return false;
01053     }
01054 
01055 #if 0
01056     fprintf(stderr, "all equal: ");
01057     v->dump();
01058     fprintf(stderr, "\n");
01059     llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
01060     if (inst) {
01061         inst->getParent()->dump();
01062         fprintf(stderr, "\n");
01063         fprintf(stderr, "\n");
01064     }
01065 #endif
01066 
01067     return false;
01068 }
01069 
01070 
01071 /** Tests to see if all of the elements of the vector in the 'v' parameter
01072     are equal.  This is a conservative test and may return false for arrays
01073     where the values are actually all equal.
01074 */
01075 bool
01076 LLVMVectorValuesAllEqual(llvm::Value *v) {
01077     llvm::VectorType *vt =
01078         llvm::dyn_cast<llvm::VectorType>(v->getType());
01079     Assert(vt != NULL);
01080     int vectorLength = vt->getNumElements();
01081 
01082     std::vector<llvm::PHINode *> seenPhis;
01083     bool equal = lVectorValuesAllEqual(v, vectorLength, seenPhis);
01084 
01085     Debug(SourcePos(), "LLVMVectorValuesAllEqual(%s) -> %s.",
01086           v->getName().str().c_str(), equal ? "true" : "false");
01087     if (g->debugPrint)
01088         LLVMDumpValue(v);
01089 
01090     return equal;
01091 }
01092 
01093 
01094 static bool
01095 lVectorIsLinear(llvm::Value *v, int vectorLength, int stride,
01096                 std::vector<llvm::PHINode *> &seenPhis);
01097 
01098 /** Given a vector of compile-time constant integer values, test to see if
01099     they are a linear sequence of constant integers starting from an
01100     arbirary value but then having a step of value "stride" between
01101     elements.
01102  */
01103 static bool
01104 lVectorIsLinearConstantInts(
01105 #ifndef LLVM_3_0
01106                             llvm::ConstantDataVector *cv, 
01107 #else
01108                             llvm::ConstantVector *cv, 
01109 #endif
01110                             int vectorLength, 
01111                             int stride) {
01112     // Flatten the vector out into the elements array
01113     llvm::SmallVector<llvm::Constant *, ISPC_MAX_NVEC> elements;
01114 #ifndef LLVM_3_0
01115     for (int i = 0; i < (int)cv->getNumElements(); ++i)
01116         elements.push_back(cv->getElementAsConstant(i));
01117 #else
01118     cv->getVectorElements(elements);
01119 #endif
01120     Assert((int)elements.size() == vectorLength);
01121 
01122     llvm::ConstantInt *ci = llvm::dyn_cast<llvm::ConstantInt>(elements[0]);
01123     if (ci == NULL)
01124         // Not a vector of integers
01125         return false;
01126 
01127     int64_t prevVal = ci->getSExtValue();
01128 
01129     // For each element in the array, see if it is both a ConstantInt and
01130     // if the difference between it and the value of the previous element
01131     // is stride.  If not, fail.
01132     for (int i = 1; i < vectorLength; ++i) {
01133         ci = llvm::dyn_cast<llvm::ConstantInt>(elements[i]);
01134         if (ci == NULL) 
01135             return false;
01136 
01137         int64_t nextVal = ci->getSExtValue();
01138         if (prevVal + stride != nextVal)
01139             return false;
01140 
01141         prevVal = nextVal;
01142     }
01143     return true;
01144 }
01145 
01146 
01147 /** Checks to see if (op0 * op1) is a linear vector where the result is a
01148     vector with values that increase by stride.
01149  */
01150 static bool
01151 lCheckMulForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, 
01152                    int stride, std::vector<llvm::PHINode *> &seenPhis) {
01153     // Is the first operand a constant integer value splatted across all of
01154     // the lanes?
01155 #ifndef LLVM_3_0
01156     llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(op0);
01157 #else
01158     llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(op0);
01159 #endif
01160     if (cv == NULL)
01161         return false;
01162 
01163     llvm::Constant *csplat = cv->getSplatValue();
01164     if (csplat == NULL)
01165         return false;
01166 
01167     llvm::ConstantInt *splat = llvm::dyn_cast<llvm::ConstantInt>(csplat);
01168     if (splat == NULL)
01169         return false;
01170 
01171     // If the splat value doesn't evenly divide the stride we're looking
01172     // for, there's no way that we can get the linear sequence we're
01173     // looking or.
01174     int64_t splatVal = splat->getSExtValue();
01175     if (splatVal == 0 || splatVal > stride || (stride % splatVal) != 0)
01176         return false;
01177 
01178     // Check to see if the other operand is a linear vector with stride
01179     // given by stride/splatVal.
01180     return lVectorIsLinear(op1, vectorLength, (int)(stride / splatVal), 
01181                            seenPhis);
01182 }
01183 
01184 
01185 /** Given (op0 AND op1), try and see if we can determine if the result is a
01186     linear sequence with a step of "stride" between values.  Returns true
01187     if so and false otherwise.  This pattern comes up when accessing SOA
01188     data.
01189  */
01190 static bool
01191 lCheckAndForLinear(llvm::Value *op0, llvm::Value *op1, int vectorLength, 
01192                    int stride, std::vector<llvm::PHINode *> &seenPhis) {
01193     // Require op1 to be a compile-time constant
01194     int64_t maskValue[ISPC_MAX_NVEC];
01195     int nElts;
01196     if (LLVMExtractVectorInts(op1, maskValue, &nElts) == false)
01197         return false;
01198     Assert(nElts == vectorLength);
01199 
01200     // Is op1 a smear of the same value across all lanes?  Give up if not.
01201     for (int i = 1; i < vectorLength; ++i)
01202         if (maskValue[i] != maskValue[0])
01203             return false;
01204 
01205     // If the op1 value isn't power of 2 minus one, then also give up.
01206     int64_t maskPlusOne = maskValue[0] + 1;
01207     bool isPowTwo = (maskPlusOne & (maskPlusOne - 1)) == 0;
01208     if (isPowTwo == false)
01209         return false;
01210 
01211     // The case we'll covert here is op0 being a linear vector with desired
01212     // stride, and where all of the values of op0, when divided by
01213     // maskPlusOne, have the same value.
01214     if (lVectorIsLinear(op0, vectorLength, stride, seenPhis) == false)
01215         return false;
01216 
01217     bool canAdd = true;
01218     bool isMult = lAllDivBaseEqual(op0, maskPlusOne, vectorLength, seenPhis,
01219                                    canAdd);
01220     return isMult;
01221 }
01222 
01223 
01224 static bool
01225 lVectorIsLinear(llvm::Value *v, int vectorLength, int stride,
01226                 std::vector<llvm::PHINode *> &seenPhis) {
01227     // First try the easy case: if the values are all just constant
01228     // integers and have the expected stride between them, then we're done.
01229 #ifndef LLVM_3_0
01230     llvm::ConstantDataVector *cv = llvm::dyn_cast<llvm::ConstantDataVector>(v);
01231 #else
01232     llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v);
01233 #endif
01234     if (cv != NULL)
01235         return lVectorIsLinearConstantInts(cv, vectorLength, stride);
01236 
01237     llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
01238     if (bop != NULL) {
01239         // FIXME: is it right to pass the seenPhis to the all equal check as well??
01240         llvm::Value *op0 = bop->getOperand(0), *op1 = bop->getOperand(1);
01241 
01242         if (bop->getOpcode() == llvm::Instruction::Add) {
01243             // There are two cases to check if we have an add:
01244             //
01245             // programIndex + unif -> ascending linear seqeuence
01246             // unif + programIndex -> ascending linear sequence
01247             bool l0 = lVectorIsLinear(op0, vectorLength, stride, seenPhis);
01248             bool e1 = lVectorValuesAllEqual(op1, vectorLength, seenPhis);
01249             if (l0 && e1)
01250                 return true;
01251 
01252             bool e0 = lVectorValuesAllEqual(op0, vectorLength, seenPhis);
01253             bool l1 = lVectorIsLinear(op1, vectorLength, stride, seenPhis);
01254             return (e0 && l1);
01255         }
01256         else if (bop->getOpcode() == llvm::Instruction::Sub)
01257             // For subtraction, we only match:
01258             // programIndex - unif -> ascending linear seqeuence
01259             return (lVectorIsLinear(bop->getOperand(0), vectorLength,
01260                                     stride, seenPhis) &&
01261                     lVectorValuesAllEqual(bop->getOperand(1), vectorLength,
01262                                           seenPhis));
01263         else if (bop->getOpcode() == llvm::Instruction::Mul) {
01264             // Multiplies are a bit trickier, so are handled in a separate
01265             // function.
01266             bool m0 = lCheckMulForLinear(op0, op1, vectorLength, stride, seenPhis);
01267             if (m0)
01268                 return true;
01269             bool m1 = lCheckMulForLinear(op1, op0, vectorLength, stride, seenPhis);
01270             return m1;
01271         }
01272         else if (bop->getOpcode() == llvm::Instruction::And) {
01273             // Special case for some AND-related patterns that come up when
01274             // looping over SOA data
01275             bool linear = lCheckAndForLinear(op0, op1, vectorLength, stride, seenPhis);
01276             return linear;
01277         }
01278         else
01279             return false;
01280     }
01281 
01282     llvm::CastInst *ci = llvm::dyn_cast<llvm::CastInst>(v);
01283     if (ci != NULL)
01284         return lVectorIsLinear(ci->getOperand(0), vectorLength,
01285                                stride, seenPhis);
01286 
01287     if (llvm::isa<llvm::CallInst>(v) || llvm::isa<llvm::LoadInst>(v))
01288         return false;
01289 
01290     llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
01291     if (phi != NULL) {
01292         for (unsigned int i = 0; i < seenPhis.size(); ++i)
01293             if (seenPhis[i] == phi)
01294                 return true;
01295 
01296         seenPhis.push_back(phi);
01297 
01298         unsigned int numIncoming = phi->getNumIncomingValues();
01299         // Check all of the incoming values: if all of them are all equal,
01300         // then we're good.
01301         for (unsigned int i = 0; i < numIncoming; ++i) {
01302             if (!lVectorIsLinear(phi->getIncomingValue(i), vectorLength, stride,
01303                                  seenPhis)) {
01304                 seenPhis.pop_back();
01305                 return false;
01306             }
01307         }
01308 
01309         seenPhis.pop_back();
01310         return true;
01311     }
01312 
01313     // TODO: is any reason to worry about these?
01314     if (llvm::isa<llvm::InsertElementInst>(v))
01315         return false;
01316 
01317     // TODO: we could also handle shuffles, but we haven't yet seen any
01318     // cases where doing so would detect cases where actually have a linear
01319     // vector.
01320     llvm::ShuffleVectorInst *shuffle = llvm::dyn_cast<llvm::ShuffleVectorInst>(v);
01321     if (shuffle != NULL)
01322         return false;
01323 
01324 #if 0
01325     fprintf(stderr, "linear check: ");
01326     v->dump();
01327     fprintf(stderr, "\n");
01328     llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
01329     if (inst) {
01330         inst->getParent()->dump();
01331         fprintf(stderr, "\n");
01332         fprintf(stderr, "\n");
01333     }
01334 #endif
01335 
01336     return false;
01337 }
01338 
01339 
01340 /** Given vector of integer-typed values, see if the elements of the array
01341     have a step of 'stride' between their values.  This function tries to
01342     handle as many possibilities as possible, including things like all
01343     elements equal to some non-constant value plus an integer offset, etc.
01344 */
01345 bool
01346 LLVMVectorIsLinear(llvm::Value *v, int stride) {
01347     llvm::VectorType *vt =
01348         llvm::dyn_cast<llvm::VectorType>(v->getType());
01349     Assert(vt != NULL);
01350     int vectorLength = vt->getNumElements();
01351 
01352     std::vector<llvm::PHINode *> seenPhis;
01353     bool linear = lVectorIsLinear(v, vectorLength, stride, seenPhis);
01354     Debug(SourcePos(), "LLVMVectorIsLinear(%s) -> %s.",
01355           v->getName().str().c_str(), linear ? "true" : "false");
01356     if (g->debugPrint)
01357         LLVMDumpValue(v);
01358 
01359     return linear;
01360 }
01361 
01362 
01363 static void
01364 lDumpValue(llvm::Value *v, std::set<llvm::Value *> &done) {
01365     if (done.find(v) != done.end())
01366         return;
01367 
01368     llvm::Instruction *inst = llvm::dyn_cast<llvm::Instruction>(v);
01369     if (done.size() > 0 && inst == NULL)
01370         return;
01371 
01372     fprintf(stderr, "  ");
01373     v->dump();
01374     done.insert(v);
01375 
01376     if (inst == NULL)
01377         return;
01378 
01379     for (unsigned i = 0; i < inst->getNumOperands(); ++i)
01380         lDumpValue(inst->getOperand(i), done);
01381 }
01382 
01383 
01384 void
01385 LLVMDumpValue(llvm::Value *v) {
01386     std::set<llvm::Value *> done;
01387     lDumpValue(v, done);
01388     fprintf(stderr, "----\n");
01389 }
01390 
01391 
01392 static llvm::Value *
01393 lExtractFirstVectorElement(llvm::Value *v, 
01394                            std::map<llvm::PHINode *, llvm::PHINode *> &phiMap) {
01395     llvm::VectorType *vt =
01396         llvm::dyn_cast<llvm::VectorType>(v->getType());
01397     Assert(vt != NULL);
01398 
01399     // First, handle various constant types; do the extraction manually, as
01400     // appropriate.
01401     if (llvm::isa<llvm::ConstantAggregateZero>(v) == true) {
01402         Assert(vt->getElementType()->isIntegerTy());
01403         return llvm::ConstantInt::get(vt->getElementType(), 0);
01404     }
01405     if (llvm::ConstantVector *cv = llvm::dyn_cast<llvm::ConstantVector>(v)) {
01406 #ifndef LLVM_3_0
01407         return cv->getOperand(0);
01408 #else
01409         llvm::SmallVector<llvm::Constant *, ISPC_MAX_NVEC> elements;
01410         cv->getVectorElements(elements);
01411         return elements[0];
01412 #endif // !LLVM_3_0
01413     }
01414 #ifndef LLVM_3_0
01415     if (llvm::ConstantDataVector *cdv = 
01416         llvm::dyn_cast<llvm::ConstantDataVector>(v))
01417         return cdv->getElementAsConstant(0);
01418 #endif  // !LLVM_3_0
01419 
01420     // Otherwise, all that we should have at this point is an instruction
01421     // of some sort
01422     Assert(llvm::isa<llvm::Constant>(v) == false);
01423     Assert(llvm::isa<llvm::Instruction>(v) == true);
01424 
01425     std::string newName = v->getName().str() + std::string(".elt0");
01426 
01427     // Rewrite regular binary operators and casts to the scalarized
01428     // equivalent.
01429     llvm::BinaryOperator *bop = llvm::dyn_cast<llvm::BinaryOperator>(v);
01430     if (bop != NULL) {
01431         llvm::Value *v0 = lExtractFirstVectorElement(bop->getOperand(0),
01432                                                      phiMap);
01433         llvm::Value *v1 = lExtractFirstVectorElement(bop->getOperand(1),
01434                                                      phiMap);
01435         // Note that the new binary operator is inserted immediately before
01436         // the previous vector one
01437         return llvm::BinaryOperator::Create(bop->getOpcode(), v0, v1,
01438                                             newName, bop);
01439     }
01440 
01441     llvm::CastInst *cast = llvm::dyn_cast<llvm::CastInst>(v);
01442     if (cast != NULL) {
01443         llvm::Value *v = lExtractFirstVectorElement(cast->getOperand(0),
01444                                                     phiMap);
01445         // Similarly, the equivalent scalar cast instruction goes right
01446         // before the vector cast
01447         return llvm::CastInst::Create(cast->getOpcode(), v,
01448                                       vt->getElementType(), newName,
01449                                       cast);
01450     }
01451 
01452     llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(v);
01453     if (phi != NULL) {
01454         // For PHI notes, recursively scalarize them.
01455         if (phiMap.find(phi) != phiMap.end())
01456             return phiMap[phi];
01457 
01458         // We need to create the new scalar PHI node immediately, though,
01459         // and put it in the map<>, so that if we come back to this node
01460         // via a recursive lExtractFirstVectorElement() call, then we can
01461         // return the pointer and not get stuck in an infinite loop.
01462         //
01463         // The insertion point for the new phi node also has to be the
01464         // start of the bblock of the original phi node.
01465         llvm::Instruction *phiInsertPos = phi->getParent()->begin();
01466         llvm::PHINode *scalarPhi = 
01467             llvm::PHINode::Create(vt->getElementType(), 
01468                                   phi->getNumIncomingValues(), 
01469                                   newName, phiInsertPos);
01470         phiMap[phi] = scalarPhi;
01471 
01472         for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
01473             llvm::Value *v = lExtractFirstVectorElement(phi->getIncomingValue(i),
01474                                                         phiMap);
01475             scalarPhi->addIncoming(v, phi->getIncomingBlock(i));
01476         }
01477 
01478         return scalarPhi;
01479     }
01480 
01481     // If we have a chain of insertelement instructions, then we can just
01482     // flatten them out and grab the value for the first one.
01483     llvm::InsertElementInst *ie = llvm::dyn_cast<llvm::InsertElementInst>(v);
01484     if (ie != NULL) {
01485         llvm::Value *elements[ISPC_MAX_NVEC];
01486         LLVMFlattenInsertChain(ie, vt->getNumElements(), elements);
01487         return elements[0];
01488     }
01489 
01490     // Worst case, for everything else, just do a regular extract element
01491     // instruction, which we insert immediately after the instruction we
01492     // have here.
01493     llvm::Instruction *insertAfter = llvm::dyn_cast<llvm::Instruction>(v);
01494     Assert(insertAfter != NULL);
01495     llvm::Instruction *ee = 
01496         llvm::ExtractElementInst::Create(v, LLVMInt32(0), "first_elt",
01497                                          (llvm::Instruction *)NULL);
01498     ee->insertAfter(insertAfter);
01499     return ee;
01500 }
01501 
01502 
01503 llvm::Value *
01504 LLVMExtractFirstVectorElement(llvm::Value *v) {
01505     std::map<llvm::PHINode *, llvm::PHINode *> phiMap;
01506     llvm::Value *ret = lExtractFirstVectorElement(v, phiMap);
01507     return ret;
01508 }
01509 
01510 
01511 /** Given two vectors of the same type, concatenate them into a vector that
01512     has twice as many elements, where the first half has the elements from
01513     the first vector and the second half has the elements from the second
01514     vector.
01515  */
01516 llvm::Value *
01517 LLVMConcatVectors(llvm::Value *v1, llvm::Value *v2, 
01518                   llvm::Instruction *insertBefore) {
01519     Assert(v1->getType() == v2->getType());
01520 
01521     llvm::VectorType *vt =
01522         llvm::dyn_cast<llvm::VectorType>(v1->getType());
01523     Assert(vt != NULL);
01524 
01525     int32_t identity[ISPC_MAX_NVEC];
01526     int resultSize = 2*vt->getNumElements();
01527     Assert(resultSize <= ISPC_MAX_NVEC);
01528     for (int i = 0; i < resultSize; ++i)
01529         identity[i] = i;
01530 
01531     return LLVMShuffleVectors(v1, v2, identity, resultSize, insertBefore);
01532 }
01533 
01534 
01535 /** Shuffle two vectors together with a ShuffleVectorInst, returning a
01536     vector with shufSize elements, where the shuf[] array offsets are used
01537     to determine which element from the two given vectors is used for each
01538     result element. */
01539 llvm::Value *
01540 LLVMShuffleVectors(llvm::Value *v1, llvm::Value *v2, int32_t shuf[],
01541                    int shufSize, llvm::Instruction *insertBefore) {
01542     std::vector<llvm::Constant *> shufVec;
01543     for (int i = 0; i < shufSize; ++i) {
01544         if (shuf[i] == -1)
01545             shufVec.push_back(llvm::UndefValue::get(LLVMTypes::Int32Type));
01546         else
01547             shufVec.push_back(LLVMInt32(shuf[i]));
01548     }
01549 
01550     llvm::ArrayRef<llvm::Constant *> aref(&shufVec[0], &shufVec[shufSize]);
01551     llvm::Value *vec = llvm::ConstantVector::get(aref);
01552 
01553     return new llvm::ShuffleVectorInst(v1, v2, vec, "shuffle", insertBefore);
01554 }
01555 
01556 
01557 const char *
01558 LLVMGetName(llvm::Value *v, const char *s) {
01559     if (v == NULL) return s;
01560     std::string ret = v->getName();
01561     ret += s;
01562     return strdup(ret.c_str());
01563 }
01564 
01565 
01566 const char *
01567 LLVMGetName(const char *op, llvm::Value *v1, llvm::Value *v2) {
01568     std::string r = op;
01569     r += "_";
01570     r += v1->getName().str();
01571     r += "_";
01572     r += v2->getName().str();
01573     return strdup(r.c_str());
01574 }
01575