llvm.org GIT mirror llvm / c8e1741
rename getConstantStringLength -> GetConstantStringInfo. Make it return the start index of the array as well as the length. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35703 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
1 changed file(s) with 84 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
383383 namespace {
384384
385385 // Forward declare utility functions.
386 static bool getConstantStringLength(Value* V, uint64_t& len,
387 ConstantArray** A = 0 );
386 static bool GetConstantStringInfo(Value *V, ConstantArray *&Array,
387 uint64_t &Length, uint64_t &StartIdx);
388388 static Value *CastToCStr(Value *V, Instruction &IP);
389389
390390 /// This LibCallOptimization will find instances of a call to "exit" that occurs
481481 // Extract the initializer (while making numerous checks) from the
482482 // source operand of the call to strcat. If we get null back, one of
483483 // a variety of checks in get_GVInitializer failed
484 uint64_t len = 0;
485 if (!getConstantStringLength(src,len))
484 uint64_t len, StartIdx;
485 ConstantArray *Arr;
486 if (!GetConstantStringInfo(src, Arr, len, StartIdx))
486487 return false;
487488
488489 // Handle the simple, do-nothing case
552553
553554 // Check that the first argument to strchr is a constant array of sbyte.
554555 // If it is, get the length and data, otherwise return false.
555 uint64_t len = 0;
556 uint64_t len, StartIdx;
556557 ConstantArray* CA = 0;
557 if (!getConstantStringLength(ci->getOperand(1), len, &CA))
558 if (!GetConstantStringInfo(ci->getOperand(1), CA, len, StartIdx))
558559 return false;
559560
560561 // Check that the second argument to strchr is a constant int. If it isn't
561 // a constant signed integer, we can try an alternate optimization
562 // a constant integer, we can try an alternate optimization
562563 ConstantInt* CSI = dyn_cast(ci->getOperand(2));
563564 if (!CSI) {
564 // The second operand is not constant, or not signed. Just lower this to
565 // The second operand is not constant just lower this to
565566 // memchr since we know the length of the string since it is constant.
566567 Constant *f = SLC.get_memchr();
567568 Value* args[3] = {
638639 }
639640
640641 bool isstr_1 = false;
641 uint64_t len_1 = 0;
642 ConstantArray* A1;
643 if (getConstantStringLength(s1,len_1,&A1)) {
642 uint64_t len_1 = 0, StartIdx;
643 ConstantArray *A1;
644 if (GetConstantStringInfo(s1, A1, len_1, StartIdx)) {
644645 isstr_1 = true;
645646 if (len_1 == 0) {
646647 // strcmp("",x) -> *x
656657 }
657658
658659 bool isstr_2 = false;
659 uint64_t len_2 = 0;
660 uint64_t len_2;
660661 ConstantArray* A2;
661 if (getConstantStringLength(s2, len_2, &A2)) {
662 if (GetConstantStringInfo(s2, A2, len_2, StartIdx)) {
662663 isstr_2 = true;
663664 if (len_2 == 0) {
664665 // strcmp(x,"") -> *x
732733 }
733734
734735 bool isstr_1 = false;
735 uint64_t len_1 = 0;
736 uint64_t len_1 = 0, StartIdx;
736737 ConstantArray* A1;
737 if (getConstantStringLength(s1, len_1, &A1)) {
738 if (GetConstantStringInfo(s1, A1, len_1, StartIdx)) {
738739 isstr_1 = true;
739740 if (len_1 == 0) {
740741 // strncmp("",x) -> *x
751752 bool isstr_2 = false;
752753 uint64_t len_2 = 0;
753754 ConstantArray* A2;
754 if (getConstantStringLength(s2,len_2,&A2)) {
755 if (GetConstantStringInfo(s2, A2, len_2, StartIdx)) {
755756 isstr_2 = true;
756757 if (len_2 == 0) {
757758 // strncmp(x,"") -> *x
820821
821822 // Get the length of the constant string referenced by the second operand,
822823 // the "src" parameter. Fail the optimization if we can't get the length
823 // (note that getConstantStringLength does lots of checks to make sure this
824 // (note that GetConstantStringInfo does lots of checks to make sure this
824825 // is valid).
825 uint64_t len = 0;
826 if (!getConstantStringLength(ci->getOperand(2),len))
826 uint64_t len, StartIdx;
827 ConstantArray *A;
828 if (!GetConstantStringInfo(ci->getOperand(2), A, len, StartIdx))
827829 return false;
828830
829831 // If the constant string's length is zero we can optimize this by just
913915 }
914916
915917 // Get the length of the constant string operand
916 uint64_t len = 0;
917 if (!getConstantStringLength(ci->getOperand(1),len))
918 uint64_t len = 0, StartIdx;
919 ConstantArray *A;
920 if (!GetConstantStringInfo(ci->getOperand(1), A, len, StartIdx))
918921 return false;
919922
920923 // strlen("xyz") -> 3 (for example)
13201323
13211324 // All the optimizations depend on the length of the first argument and the
13221325 // fact that it is a constant string array. Check that now
1323 uint64_t len = 0;
1326 uint64_t len, StartIdx;
13241327 ConstantArray* CA = 0;
1325 if (!getConstantStringLength(ci->getOperand(1), len, &CA))
1328 if (!GetConstantStringInfo(ci->getOperand(1), CA, len, StartIdx))
13261329 return false;
13271330
13281331 if (len != 2 && len != 3)
13981401
13991402 // All the optimizations depend on the length of the second argument and the
14001403 // fact that it is a constant string array. Check that now
1401 uint64_t len = 0;
1404 uint64_t len, StartIdx;
14021405 ConstantArray* CA = 0;
1403 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1406 if (!GetConstantStringInfo(ci->getOperand(2), CA, len, StartIdx))
14041407 return false;
14051408
14061409 if (ci->getNumOperands() == 3) {
14501453 switch (CI->getZExtValue()) {
14511454 case 's':
14521455 {
1453 uint64_t len = 0;
1456 uint64_t len, StartIdx;
14541457 ConstantArray* CA = 0;
1455 if (getConstantStringLength(ci->getOperand(3), len, &CA)) {
1458 if (GetConstantStringInfo(ci->getOperand(3), CA, len, StartIdx)) {
14561459 // fprintf(file,"%s",str) -> fwrite(str,strlen(str),1,file)
14571460 const Type* FILEptr_type = ci->getOperand(1)->getType();
14581461 Value* args[4] = {
15151518
15161519 // All the optimizations depend on the length of the second argument and the
15171520 // fact that it is a constant string array. Check that now
1518 uint64_t len = 0;
1521 uint64_t len, StartIdx;
15191522 ConstantArray* CA = 0;
1520 if (!getConstantStringLength(ci->getOperand(2), len, &CA))
1523 if (!GetConstantStringInfo(ci->getOperand(2), CA, len, StartIdx))
15211524 return false;
15221525
15231526 if (ci->getNumOperands() == 3) {
16401643
16411644 // All the optimizations depend on the length of the first argument and the
16421645 // fact that it is a constant string array. Check that now
1643 uint64_t len = 0;
1644 if (!getConstantStringLength(ci->getOperand(1), len))
1646 uint64_t len, StartIdx;
1647 ConstantArray *CA;
1648 if (!GetConstantStringInfo(ci->getOperand(1), CA, len, StartIdx))
16451649 return false;
16461650
16471651 switch (len) {
19871991 }
19881992 } NearByIntOptimizer;
19891993
1990 /// A function to compute the length of a null-terminated constant array of
1991 /// integers. This function can't rely on the size of the constant array
1992 /// because there could be a null terminator in the middle of the array.
1994 /// GetConstantStringInfo - This function computes the length of a
1995 /// null-terminated constant array of integers. This function can't rely on the
1996 /// size of the constant array because there could be a null terminator in the
1997 /// middle of the array.
1998 ///
19931999 /// We also have to bail out if we find a non-integer constant initializer
19942000 /// of one of the elements or if there is no null-terminator. The logic
19952001 /// below checks each of these conditions and will return true only if all
1996 /// conditions are met. In that case, the \p len parameter is set to the length
1997 /// of the null-terminated string. If false is returned, the conditions were
1998 /// not met and len is set to 0.
1999 /// @brief Get the length of a constant string (null-terminated array).
2000 static bool getConstantStringLength(Value *V, uint64_t &len, ConstantArray **CA)
2001 {
2002 assert(V != 0 && "Invalid args to getConstantStringLength");
2003 len = 0; // make sure we initialize this
2004 User* GEP = 0;
2002 /// conditions are met. If the conditions aren't met, this returns false.
2003 ///
2004 /// If successful, the \p Array param is set to the constant array being
2005 /// indexed, the \p Length parameter is set to the length of the null-terminated
2006 /// string pointed to by V, the \p StartIdx value is set to the first
2007 /// element of the Array that V points to, and true is returned.
2008 static bool GetConstantStringInfo(Value *V, ConstantArray *&Array,
2009 uint64_t &Length, uint64_t &StartIdx) {
2010 assert(V != 0 && "Invalid args to GetConstantStringInfo");
2011 // Initialize results.
2012 Length = 0;
2013 StartIdx = 0;
2014 Array = 0;
2015
2016 User *GEP = 0;
20052017 // If the value is not a GEP instruction nor a constant expression with a
20062018 // GEP instruction, then return false because ConstantArray can't occur
20072019 // any other way
2008 if (GetElementPtrInst* GEPI = dyn_cast(V))
2020 if (GetElementPtrInst *GEPI = dyn_cast(V)) {
20092021 GEP = GEPI;
2010 else if (ConstantExpr* CE = dyn_cast(V))
2011 if (CE->getOpcode() == Instruction::GetElementPtr)
2012 GEP = CE;
2013 else
2014 return false;
2015 else
2022 } else if (ConstantExpr *CE = dyn_cast(V)) {
2023 if (CE->getOpcode() != Instruction::GetElementPtr)
2024 return false;
2025 GEP = CE;
2026 } else {
20162027 return false;
2028 }
20172029
20182030 // Make sure the GEP has exactly three arguments.
20192031 if (GEP->getNumOperands() != 3)
20272039 } else
20282040 return false;
20292041
2030 // Ensure that the second operand is a ConstantInt. If it isn't then this
2031 // GEP is wonky and we're not really sure what were referencing into and
2032 // better of not optimizing it. While we're at it, get the second index
2033 // value. We'll need this later for indexing the ConstantArray.
2034 uint64_t start_idx = 0;
2035 if (ConstantInt* CI = dyn_cast(GEP->getOperand(2)))
2036 start_idx = CI->getZExtValue();
2042 // If the second index isn't a ConstantInt, then this is a variable index
2043 // into the array. If this occurs, we can't say anything meaningful about
2044 // the string.
2045 StartIdx = 0;
2046 if (ConstantInt *CI = dyn_cast(GEP->getOperand(2)))
2047 StartIdx = CI->getZExtValue();
20372048 else
20382049 return false;
20392050
20432054 GlobalVariable* GV = dyn_cast(GEP->getOperand(0));
20442055 if (!GV || !GV->isConstant() || !GV->hasInitializer())
20452056 return false;
2046
2047 // Get the initializer.
2048 Constant* INTLZR = GV->getInitializer();
2057 Constant *GlobalInit = GV->getInitializer();
20492058
20502059 // Handle the ConstantAggregateZero case
2051 if (isa(INTLZR)) {
2060 if (isa(GlobalInit)) {
20522061 // This is a degenerate case. The initializer is constant zero so the
20532062 // length of the string must be zero.
2054 len = 0;
2063 Length = 0;
20552064 return true;
20562065 }
20572066
20582067 // Must be a Constant Array
2059 ConstantArray* A = dyn_cast(INTLZR);
2060 if (!A)
2061 return false;
2068 Array = dyn_cast(GlobalInit);
2069 if (!Array) return false;
20622070
20632071 // Get the number of elements in the array
2064 uint64_t max_elems = A->getType()->getNumElements();
2072 uint64_t NumElts = Array->getType()->getNumElements();
20652073
20662074 // Traverse the constant array from start_idx (derived above) which is
20672075 // the place the GEP refers to in the array.
2068 for (len = start_idx; len < max_elems; len++) {
2069 if (ConstantInt *CI = dyn_cast(A->getOperand(len))) {
2070 // Check for the null terminator
2076 Length = StartIdx;
2077 while (1) {
2078 if (Length >= NumElts)
2079 return false; // The array isn't null terminated.
2080
2081 Constant *Elt = Array->getOperand(Length);
2082 if (ConstantInt *CI = dyn_cast(Elt)) {
2083 // Check for the null terminator.
20712084 if (CI->isZero())
20722085 break; // we found end of string
20732086 } else
20742087 return false; // This array isn't suitable, non-int initializer
2088 ++Length;
20752089 }
20762090
2077 if (len >= max_elems)
2078 return false; // This array isn't null terminated
2079
20802091 // Subtract out the initial value from the length
2081 len -= start_idx;
2082 if (CA)
2083 *CA = A;
2092 Length -= StartIdx;
20842093 return true; // success!
20852094 }
20862095